Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to store values of selected checkboxes in array in React js?

I make table contain students names, and checkbox for every row in the table, I want so store the students names that checked in the (students) array. How can I do that?

const [data, setData] = useState([]);  
    const [students, setStudents] = useState([]);  

    
  useEffect(() => {  
    Axios  
        .get("http://localhost:3003/studentsnotinsections")  
        .then(result => setData(result.data));  
}, []);  

console.log(students)
return(
  <div>
 <table className="table" >  
                <thead className="thead-dark">  
                    <tr>  
                        <th scope="col">Name</th>  
                    </tr>  
                </thead>  
                <tbody>                      
                    {data.map((item, id) => {  
                        return <tr key={id}>
                            <td>{item.FullName}</td> 
                            <td><input type="checkbox" /></td> 
                        </tr>  
                    })}  
                </tbody>  
            </table>
  </div>
)}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Did you try simply adding onclick function to your checkbox?

const Compon = () => {
  const [data, setData] = useState([]);  
  const [students, setStudents] = useState([]);  
  
      
  useEffect(() => {  
    Axios  
        .get("http://localhost:3003/studentsnotinsections")  
        .then(result => setData(result.data));  
  }, []);  
  const addOrRemove = (name) => {
    const newStudents = [...students];
    const index = newStudents.indexOf(name);
    if (index === -1) {
      newStudents.push(name);
    } else {
      newStudents.splice(index, 1);
    }
    setStudents(newStudents);
    console.log(students)
  }
  
  return(
    <div>
     <table className="table" >  
        <thead className="thead-dark">  
            <tr>  
                <th scope="col">Name</th>  
            </tr>  
         </thead>  
        <tbody>                      
            {data.map((item, id) => {  
                return <tr key={id}>
                    <td>{item.FullName}</td> 
                    <td><input type="checkbox" onClick={() => addOrRemove(item.FullName)} /></td> 
                </tr>  
            })}  
        </tbody>  
      </table>
     </div>
  )
  }

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading