mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-24 09:25:15 +00:00
28 lines
897 B
JavaScript
28 lines
897 B
JavaScript
|
import { useReducer } from 'react';
|
||
|
/**
|
||
|
* Returns a function that triggers a component update. the hook equivalent to
|
||
|
* `this.forceUpdate()` in a class component. In most cases using a state value directly
|
||
|
* is preferable but may be required in some advanced usages of refs for interop or
|
||
|
* when direct DOM manipulation is required.
|
||
|
*
|
||
|
* ```ts
|
||
|
* const forceUpdate = useForceUpdate();
|
||
|
*
|
||
|
* const updateOnClick = useCallback(() => {
|
||
|
* forceUpdate()
|
||
|
* }, [forceUpdate])
|
||
|
*
|
||
|
* return <button type="button" onClick={updateOnClick}>Hi there</button>
|
||
|
* ```
|
||
|
*/
|
||
|
|
||
|
export default function useForceUpdate() {
|
||
|
// The toggling state value is designed to defeat React optimizations for skipping
|
||
|
// updates when they are stricting equal to the last state value
|
||
|
var _useReducer = useReducer(function (state) {
|
||
|
return !state;
|
||
|
}, false),
|
||
|
dispatch = _useReducer[1];
|
||
|
|
||
|
return dispatch;
|
||
|
}
|