main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
994 bytes
|
Rev | Line | |
---|
[d565449] | 1 | import { __assign } from "tslib";
|
---|
| 2 | import { useCallback, useRef } from 'react';
|
---|
| 3 | import useUpdate from './useUpdate';
|
---|
| 4 | var useGetSetState = function (initialState) {
|
---|
| 5 | if (initialState === void 0) { initialState = {}; }
|
---|
| 6 | if (process.env.NODE_ENV !== 'production') {
|
---|
| 7 | if (typeof initialState !== 'object') {
|
---|
| 8 | console.error('useGetSetState initial state must be an object.');
|
---|
| 9 | }
|
---|
| 10 | }
|
---|
| 11 | var update = useUpdate();
|
---|
| 12 | var state = useRef(__assign({}, initialState));
|
---|
| 13 | var get = useCallback(function () { return state.current; }, []);
|
---|
| 14 | var set = useCallback(function (patch) {
|
---|
| 15 | if (!patch) {
|
---|
| 16 | return;
|
---|
| 17 | }
|
---|
| 18 | if (process.env.NODE_ENV !== 'production') {
|
---|
| 19 | if (typeof patch !== 'object') {
|
---|
| 20 | console.error('useGetSetState setter patch must be an object.');
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
| 23 | Object.assign(state.current, patch);
|
---|
| 24 | update();
|
---|
| 25 | }, []);
|
---|
| 26 | return [get, set];
|
---|
| 27 | };
|
---|
| 28 | export default useGetSetState;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.