Last active
January 14, 2021 19:08
-
-
Save vzaidman/c059e17f097bdc68dd58fff829c1d952 to your computer and use it in GitHub Desktop.
useState-as-a-ref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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