Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to determine checkbox checked in react (useState)

This is my first time to develop a react application. I tried to implement checkboxes in each row of a table and check which of the rows are selected.

I used useState hook to make checked and onChange events, but seems the values are not refreshing when I check then uncheck the checkbox.

I would like to ask how to add a logic to remove the unticked values on the hooks.

T1 Checkbox A- Checked Checkbox B- Checked CheckedMap- A,B

T2 Checkbox B- Unchecked CheckedMap- A,B // Unticked Checkbox B is also stored in CheckedMap

Thank you for your help.

export default function({ infinite }) {
  const [checkedMap, setCheckedMap] = useState(new Map());
}

const handleCheckedChange = transaction_seq => {
    let modifiedMap = checkedMap;
    modifiedMap.set(transaction_seq, !checkedMap.get(transaction_seq));
    setCheckedMap(modifiedMap);
  };

const columns = [
    {
      Header: "Transaction(s)",
      className: "left",
      columns: [
        {
          id: "checkbox",
          accessor: "checkbox",
          Cell: ({ row }) => {
            return (
              <input
                type="checkbox"
                className="checkbox"
                checked={checkedMap.get(row.original.transaction_seq)}
                onChange={() =>
                  handleCheckedChange(row.original.transaction_seq)
                }
like image 880
Dan Avatar asked May 29 '19 09:05

Dan


People also ask

How do you know which checkbox is checked in React?

Use the target. checked property on the event object to check if a checkbox is checked in React, e.g. if (event. target. checked) {} .

How do you know if a checkbox is checked?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

How do you know how many checkboxes are checked in React JS?

You need to store information about whether input is "checked" in your data. Then, simply count items with truthy "checked" flag.


2 Answers

This is the way how to use controlled checkbox with react useState hook.

const { useState } = React; // --> for inline use
// import React, { useState } from 'react';  // --> for real project


const App = () => {
  const [checked, setChecked] = useState(false)
  const handleClick = () => setChecked(!checked)
  
  return <input onClick={handleClick} checked={checked} type="checkbox" />
};


ReactDOM.render(<App />, document.getElementById('root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.9.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.9.0/umd/react-dom.production.min.js"></script>
<div id="root"></div>
like image 111
gazdagergo Avatar answered Oct 12 '22 12:10

gazdagergo


You may get an error using onClick for the function. Instead using onChange:

    import { useState} from "react";

    export default function App() {
      const [checked, setChecked] = useState<boolean>(true);
      
      return (
        <input name="checked" type="checkbox" checked={checked} 
          onChange={e => setChecked(!checked)} />
      )
    }
like image 24
Brian Avatar answered Oct 12 '22 12:10

Brian