Categories
React Answers

How to get checkbox value in React.js?

Spread the love

To get checkbox value in React.js, we can use a state.

For instance, we write

const Component = () => {
  const [value, setvalue] = useState(false);

  return (
    <div className="form-check">
      <input
        className="form-check-input"
        checked={value}
        onChange={(e) => setValue(e.target.checked)}
        type="checkbox"
      />
      <label className="form-check-label">chechbox value</label>
      {value}
    </div>
  );
};

to set the onChange prop of the checkbox input to a function that calls setValue with the checked value of the checkbox.

Then we get the checked value from the value state and set that as the checked prop’s value to update the checkbox with the latest checked value.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *