Skip to content

Instantly share code, notes, and snippets.

@joshblack
Created August 1, 2019 21:43
Show Gist options
  • Save joshblack/3132e9bb8b4da9821e3425ccebac8d0a to your computer and use it in GitHub Desktop.
Save joshblack/3132e9bb8b4da9821e3425ccebac8d0a to your computer and use it in GitHub Desktop.
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