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:
927 bytes
|
Rev | Line | |
---|
[d565449] | 1 | import { createContext, createElement, useContext, useReducer } from 'react';
|
---|
| 2 | var createReducerContext = function (reducer, defaultInitialState) {
|
---|
| 3 | var context = createContext(undefined);
|
---|
| 4 | var providerFactory = function (props, children) { return createElement(context.Provider, props, children); };
|
---|
| 5 | var ReducerProvider = function (_a) {
|
---|
| 6 | var children = _a.children, initialState = _a.initialState;
|
---|
| 7 | var state = useReducer(reducer, initialState !== undefined ? initialState : defaultInitialState);
|
---|
| 8 | return providerFactory({ value: state }, children);
|
---|
| 9 | };
|
---|
| 10 | var useReducerContext = function () {
|
---|
| 11 | var state = useContext(context);
|
---|
| 12 | if (state == null) {
|
---|
| 13 | throw new Error("useReducerContext must be used inside a ReducerProvider.");
|
---|
| 14 | }
|
---|
| 15 | return state;
|
---|
| 16 | };
|
---|
| 17 | return [useReducerContext, ReducerProvider, context];
|
---|
| 18 | };
|
---|
| 19 | export default createReducerContext;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.