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