main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 6 months ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
889 bytes
|
Rev | Line | |
---|
[d565449] | 1 | import { createContext, createElement, useContext, useState } from 'react';
|
---|
| 2 | var createStateContext = function (defaultInitialValue) {
|
---|
| 3 | var context = createContext(undefined);
|
---|
| 4 | var providerFactory = function (props, children) { return createElement(context.Provider, props, children); };
|
---|
| 5 | var StateProvider = function (_a) {
|
---|
| 6 | var children = _a.children, initialValue = _a.initialValue;
|
---|
| 7 | var state = useState(initialValue !== undefined ? initialValue : defaultInitialValue);
|
---|
| 8 | return providerFactory({ value: state }, children);
|
---|
| 9 | };
|
---|
| 10 | var useStateContext = function () {
|
---|
| 11 | var state = useContext(context);
|
---|
| 12 | if (state == null) {
|
---|
| 13 | throw new Error("useStateContext must be used inside a StateProvider.");
|
---|
| 14 | }
|
---|
| 15 | return state;
|
---|
| 16 | };
|
---|
| 17 | return [useStateContext, StateProvider, context];
|
---|
| 18 | };
|
---|
| 19 | export default createStateContext;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.