Skip to content

Instantly share code, notes, and snippets.

@vzaidman
Last active January 14, 2021 19:08
Show Gist options
  • Save vzaidman/c059e17f097bdc68dd58fff829c1d952 to your computer and use it in GitHub Desktop.
Save vzaidman/c059e17f097bdc68dd58fff829c1d952 to your computer and use it in GitHub Desktop.
useState-as-a-ref
const [node, setRef] = useState(null);
useEffect(() => {
if (!node) {
console.log('unmounted!');
return null;
}
console.log('mounted');
const fn = e => console.log(e);
node.addEventListener('mousedown', fn);
return () => node.removeEventListener('mousedown', fn);
}, [node])
// <div ref={setRef}....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment