Created
October 22, 2019 09:46
-
-
Save bittu/967b3e7361dba70f4dece38ee611895a to your computer and use it in GitHub Desktop.
`forceUpdate` hook for React functional components
This file contains hidden or 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 { useState, useCallback } from 'react' | |
export default function useForceUpdate() { | |
const [, dispatch] = useState({}) | |
return useCallback(() => dispatch({}), []) | |
} | |
/** | |
* Usage: | |
* | |
* const functionalComponent = (props) => { | |
* const forceUpdate = useForceUpdate() | |
* | |
* return ( | |
* <button onClick={forceUpdate}>Force update</button> | |
* ) | |
* } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment