Skip to content

Instantly share code, notes, and snippets.

@timveletta
Created July 13, 2020 09:44
Show Gist options
  • Save timveletta/b169678bdc624ea38bb8161214ff1181 to your computer and use it in GitHub Desktop.
Save timveletta/b169678bdc624ea38bb8161214ff1181 to your computer and use it in GitHub Desktop.
How to access React state when cleaning up your component with hooks.
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