-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (64 loc) · 2.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, { createContext, useContext, useReducer } from "react";
import PropTypes from "prop-types";
export const StateContext = createContext();
/**
* Wrap this component around main App content
* @example
<StateProvider initialState={initialState} reducer={reducer}> App </StateProvider>
*/
export const StateProvider = ({ reducer, initialState, children }) => (
<StateContext.Provider value={useReducer(reducer, initialState)}>
{children}
</StateContext.Provider>
);
/**
* use this Hook inside your any component to access your store
* @example
const [{ user }, dispatch] = useStateValue();
*/
export const useStateValue = () => useContext(StateContext);
/**
* if you have more than one reducer use this function to create root_reducer, we mix all of your reducers into one reducer function
* @param {object} reducers
* @example
const root_reducer = combineReducers({user: reducer_user,items: reducer_items});
*/
export const combineReducers = reducers => {
// First get an array with all the keys of the reducers (the reducer names)
const reducerKeys = Object.keys(reducers);
return function combination(state = {}, action) {
// This is the object we are going to return.
const nextState = {};
// Loop through all the reducer keys
for (let i = 0; i < reducerKeys.length; i++) {
// Get the current key name
const key = reducerKeys[i];
// Get the current reducer
const reducer = reducers[key];
// Get the the previous state
const previousStateForKey = state[key];
// Get the next state by running the reducer
const nextStateForKey = reducer(previousStateForKey, action);
// Update the new state for the current reducer
nextState[key] = nextStateForKey;
}
return nextState;
};
};
// --- propTypes
StateProvider.propTypes = {
/**
* @return {React.Node}
*/
children: PropTypes.node.isRequired,
/**
* Object containing initial state value.
*/
initialState: PropTypes.shape({}).isRequired,
/**
*
* @param {object} state
* @param {object} action
*/
reducer: PropTypes.func.isRequired
};