1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
4 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.default = useSyncState;
|
9 | var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
10 | var React = _interopRequireWildcard(require("react"));
|
11 | var _useEvent = _interopRequireDefault(require("./useEvent"));
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function useSyncState(defaultValue) {
|
18 | var _React$useReducer = React.useReducer(function (x) {
|
19 | return x + 1;
|
20 | }, 0),
|
21 | _React$useReducer2 = (0, _slicedToArray2.default)(_React$useReducer, 2),
|
22 | forceUpdate = _React$useReducer2[1];
|
23 | var currentValueRef = React.useRef(defaultValue);
|
24 | var getValue = (0, _useEvent.default)(function () {
|
25 | return currentValueRef.current;
|
26 | });
|
27 | var setValue = (0, _useEvent.default)(function (updater) {
|
28 | currentValueRef.current = typeof updater === 'function' ? updater(currentValueRef.current) : updater;
|
29 | forceUpdate();
|
30 | });
|
31 | return [getValue, setValue];
|
32 | } |
\ | No newline at end of file |