dimanche 15 août 2021

Push Multiple checkbox data in array in ReactJs?

I need to push the data in array form, from checkbox value.

Code

import React,{ useState,useEffect } from "react";

export default function App() {

  const [person, setPerson] = useState([]);

  const CheckHandler = () => {
  //  setPerson(() => {     ...person, }) 

  }

  useEffect(() => {
  //Print data each time the checkbox is "checked" or "unchecked"
    console.log(person);
  }, [person]);


  return (
    <>
    <input type="checkbox" id="jane" name="jane" value="jane" onClick={() => CheckHandler()} />
    <label htmlFor="jane">jane</label><br/>

    <input type="checkbox" id="Mike" name="Mike" value="Mike" onClick={() => CheckHandler()} />
    <label htmlFor="Mike">Mike</label><br/>

    <input type="checkbox" id="board" name="board" value="board" onClick={() => CheckHandler()} />
    <label htmlFor="board">board</label><br/>
    
    </>
  );
}

What should I do to get my data in array in my console?

sandbox : https://codesandbox.io/s/adoring-rgb-27wkt




Aucun commentaire:

Enregistrer un commentaire