Skip to content

Instantly share code, notes, and snippets.

@bittu
Created October 22, 2019 09:46
Show Gist options
  • Save bittu/967b3e7361dba70f4dece38ee611895a to your computer and use it in GitHub Desktop.
Save bittu/967b3e7361dba70f4dece38ee611895a to your computer and use it in GitHub Desktop.
`forceUpdate` hook for React functional components
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