-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppContext.js
71 lines (61 loc) · 2.44 KB
/
AppContext.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
// AppContext.js
import React, { createContext, useContext, useReducer } from 'react';
const AppContext = createContext();
const initialState = { //initialState is the initial state of the application. It is an object with the following properties(categories) and their initial values:
personalInfo: [],
sports: [],
educations: [],
volunteerServices: [],
ecs: [],
acs: [],
sas: [],
mas: [],
leaderships: [],
hcs: [],
aI: [],
};
const reducer = (state, action) => {
//reducer is a function that takes the current state and an action as arguments and returns a new state based on that action. It is a pure function that only depends on the arguments passed to it. It does not depend on any external state or change its arguments.
switch (action.type) {
case 'SET_PERSONALINFO':
return { ...state, personalInfo: action.payload };
case 'SET_SPORTS':
return { ...state, sports: action.payload };
case 'SET_EDUCATIONS':
return { ...state, educations: action.payload };
case 'SET_VOLUNTEERSERVICES':
return { ...state, volunteerServices: action.payload };
case 'SET_ECS':
return { ...state, ecs: action.payload };
case 'SET_ACS':
return { ...state, acs: action.payload };
case 'SET_SAS':
return { ...state, sas: action.payload };
case 'SET_MAS':
return { ...state, mas: action.payload };
case 'SET_LEADERSHIPS':
return { ...state, leaderships: action.payload };
case 'SET_HCS':
return { ...state, hcs: action.payload };
case 'SET_AI':
return { ...state, aI: action.payload };
default:
return state;
}
};
const AppProvider = ({ children }) => { //AppProvider is a component that provides the application state to all its children. It takes the children as a prop and returns the AppContext.Provider component with the value prop set to the current state and the dispatch function.
const [state, dispatch] = useReducer(reducer, initialState);
return (
<AppContext.Provider value={{ state, dispatch }}>
{children}
</AppContext.Provider>
);
};
const useAppContext = () => { //useAppContext is a custom hook that returns the current state and the dispatch function. It is a wrapper around the useContext hook.
const context = useContext(AppContext);
if (!context) {
throw new Error('useAppContext must be used within an AppProvider');
}
return context;
};
export { AppProvider, useAppContext };