Created
August 1, 2019 21:43
-
-
Save joshblack/3132e9bb8b4da9821e3425ccebac8d0a 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
import { useReducer } from 'react'; | |
function forcedReducer(state) { | |
return !state; | |
} | |
function useForceUpdate() { | |
return useReducer(forcedReducer, false)[1]; | |
} | |
// Usage | |
function MyComponent() { | |
const forceUpdate = useForceUpdate(); | |
function onClick() { | |
forceUpdate(); | |
} | |
return <button onClick={onClick}>Hello</button> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment