Last active
May 20, 2020 14:01
-
-
Save ryanflorence/02162f8660201987bf9fde0172aa75c6 to your computer and use it in GitHub Desktop.
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
// I've only used this for focus management, I'm willing to bet you don't | |
// actually want to do this unless it's focus management | |
function useUpdateEffect(effect, deps) { | |
let mounted = useRef(false) | |
useEffect(() => { | |
if (mounted.current) { | |
return effect() | |
} else { | |
mounted.current = true | |
} | |
}, deps) // linter will hate you but whatev | |
} |
Author
ryanflorence
commented
May 19, 2020
Could do it this way too:
function Thing({ code }) {
let firstCode = useRef(code)
useEffect(() => {
if (code === firstCode.current) {
return
} else {
doYourThing()
}
}, [code])
}
Oh, duh, that is exactly what you had in your tweet.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment