UNPKG

906 BJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = useForceUpdate;
5var _react = require("react");
6/**
7 * Returns a function that triggers a component update. the hook equivalent to
8 * `this.forceUpdate()` in a class component. In most cases using a state value directly
9 * is preferable but may be required in some advanced usages of refs for interop or
10 * when direct DOM manipulation is required.
11 *
12 * ```ts
13 * const forceUpdate = useForceUpdate();
14 *
15 * const updateOnClick = useCallback(() => {
16 * forceUpdate()
17 * }, [forceUpdate])
18 *
19 * return <button type="button" onClick={updateOnClick}>Hi there</button>
20 * ```
21 */
22function useForceUpdate() {
23 // The toggling state value is designed to defeat React optimizations for skipping
24 // updates when they are strictly equal to the last state value
25 const [, dispatch] = (0, _react.useReducer)(state => !state, false);
26 return dispatch;
27}
\No newline at end of file