Created
July 22, 2020 17:58
-
-
Save kyleshevlin/cb4ab88ee4b06d764636cc84c8290f2c to your computer and use it in GitHub Desktop.
useForceUpdate
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
function useForceUpdate() { | |
const [, setState] = React.useState(true) | |
return () => setState(s => !s) | |
} | |
function Toggle() { | |
const forceUpdate = useForceUpdate() | |
const value = React.useRef(false) | |
const handleClick = () => { | |
value.current = !value.current | |
forceUpdate() | |
} | |
return ( | |
<> | |
<div>{String(value.current)}</div> | |
<button onClick={handleClick} type="button">toggle</button> | |
</> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment