Created
July 13, 2020 09:44
-
-
Save timveletta/b169678bdc624ea38bb8161214ff1181 to your computer and use it in GitHub Desktop.
How to access React state when cleaning up your component with hooks.
This file contains 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 [value, setValue] = useState(); | |
const valueRef = useRef(); | |
useEffect(() => { | |
valueRef.current = value; | |
}, [value]); | |
useEffect(() => { | |
return function cleanup () { | |
console.log(valueRef.current) | |
} | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment