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

PHOTO EMBED

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

Saved by @EMR4HKLMN #jsx

import {useRef} from 'react';

export default function App() {
  const ref = useRef(null);

  const handleClick = () => {
    // 👇️ get checkbox value with ref
    console.log(ref.current.checked);
  };

  return (
    <div>
      <input
       ref={ref}
       type="checkbox"
       id="subscribe"
       name="subscribe"
       defaultChecked={true}
      />

      <button onClick={handleClick}>Click</button>
    </div>
  );
}
content_copyCOPY

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