Last active
October 31, 2022 18:43
-
-
Save frzi/c9de4fa77f59ff1fb465265e0b227761 to your computer and use it in GitHub Desktop.
useForceUpdate in React 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
// Put me somewhere accessible. | |
const useForceUpdate = () => { | |
const [_, setState] = useState(0) | |
return () => setState(val => val + 1) | |
} | |
// Example. | |
const Component = () => { | |
const forceUpdate = useForceUpdate() | |
const onClick = () => forceUpdate() | |
return ( | |
<div onClick={onClick}> | |
{Date.now()} | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep! I actually ran into a similar issue as a matter of fact. Completely forgot I still had this gist somewhere rotting, collecting dust.
The fix is enitrely the same as yours:
Thanks for reminding me this broken piece of code was still publicly available. I'll update it 😄