Force a React component to refresh

by michael
~1 minute
Force a React component to refresh

Sometimes you just want a component to redraw and if it isn't triggered by itself, this little snippet may do the trick:

const someComponent = ()=>{
    const [, updateState] = React.useState()
    const forceUpdate = React.useCallback(() => updateState({}), [])

    forceUpdate()
}

Changing state triggers a redraw. And that's what we enforce here.

Currently there are no comments, so be the first!