1 | import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
2 | import * as React from 'react';
|
3 | export default function useSyncState(defaultState, onChange) {
|
4 | var stateRef = React.useRef(defaultState);
|
5 |
|
6 | var _React$useState = React.useState({}),
|
7 | _React$useState2 = _slicedToArray(_React$useState, 2),
|
8 | forceUpdate = _React$useState2[1];
|
9 |
|
10 | function setState(updater) {
|
11 | var newValue = typeof updater === 'function' ? updater(stateRef.current) : updater;
|
12 |
|
13 | if (newValue !== stateRef.current) {
|
14 | onChange(newValue, stateRef.current);
|
15 | }
|
16 |
|
17 | stateRef.current = newValue;
|
18 | forceUpdate({});
|
19 | }
|
20 |
|
21 | return [stateRef.current, setState];
|
22 | } |
\ | No newline at end of file |