Skip to content

Instantly share code, notes, and snippets.

@kyleshevlin
Created July 22, 2020 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleshevlin/cb4ab88ee4b06d764636cc84c8290f2c to your computer and use it in GitHub Desktop.
Save kyleshevlin/cb4ab88ee4b06d764636cc84c8290f2c to your computer and use it in GitHub Desktop.
useForceUpdate
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