forked from MitchelSt/context-api-snippet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippet
37 lines (31 loc) · 812 Bytes
/
snippet
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
import React, { createContext, useReducer } from "react";
const initialState = {
inputValue: 0,
};
const reducer = (state, action) => {
const { type, payload } = action;
switch (type) {
case "SET_INPUT_VALUE":
return {
...state,
inputValue: payload,
};
case "SET_INPUT_VALUE_TO_100":
return {
...state,
inputValue: 100,
};
default:
return state;
}
};
const InputValueContext = createContext({ state: initialState, dispatch: () => {} });
function InputValueProvider({ children }) {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<InputValueContext.Provider value={{ state, dispatch }}>
{children}
</InputValueContext.Provider>
);
}
export { InputValueContext, InputValueProvider };