@restart/hooks
Version:
A set of utility and general-purpose React hooks.
27 lines (26 loc) • 906 B
JavaScript
exports.__esModule = true;
exports.default = useForceUpdate;
var _react = require("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>
* ```
*/
function useForceUpdate() {
// The toggling state value is designed to defeat React optimizations for skipping
// updates when they are strictly equal to the last state value
const [, dispatch] = (0, _react.useReducer)(state => !state, false);
return dispatch;
}
;