UNPKG

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