1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.default = useMergeState;
|
5 | var _react = require("react");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function useMergeState(initialState) {
|
26 | const [state, setState] = (0, _react.useState)(initialState);
|
27 | const updater = (0, _react.useCallback)(update => {
|
28 | if (update === null) return;
|
29 | if (typeof update === 'function') {
|
30 | setState(state => {
|
31 | const nextState = update(state);
|
32 | return nextState == null ? state : Object.assign({}, state, nextState);
|
33 | });
|
34 | } else {
|
35 | setState(state => Object.assign({}, state, update));
|
36 | }
|
37 | }, [setState]);
|
38 | return [state, updater];
|
39 | } |
\ | No newline at end of file |