Get the value of a Checkbox using a ref in React | bobbyhadz

PHOTO EMBED

Fri Oct 28 2022 15:00:10 GMT+0000 (Coordinated Universal Time)

Saved by @EMR4HKLMN #jsx

import {useState} from 'react';

export default function App() {
  const [isSubscribed, setIsSubscribed] = useState(false);

  const handleChange = event => {
    setIsSubscribed(event.target.checked);

    // 👇️ this is the checkbox itself
    console.log(event.target);

    // 👇️ this is the checked value of the field
    console.log(event.target.checked);
  };

  return (
    <div>
      <label htmlFor="subscribe">Subscribe</label>
      <input
        type="checkbox"
        id="subscribe"
        name="subscribe"
        onChange={handleChange}
        checked={isSubscribed}
      />
    </div>
  );
}
content_copyCOPY

https://bobbyhadz.com/blog/react-ref-checkbox