36 lines
811 B
JavaScript
36 lines
811 B
JavaScript
import React, {createContext, useContext, useReducer} from 'react';
|
|
|
|
const initialState = {user: {auth: false}};
|
|
|
|
const reducer = (state, action) => {
|
|
console.log(state, action);
|
|
|
|
switch (action.type) {
|
|
case 'login':
|
|
return {
|
|
...state,
|
|
user: action.user
|
|
}
|
|
case 'logout':
|
|
return {
|
|
...state,
|
|
user: action.user
|
|
}
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
export const StateContext = createContext(null);
|
|
|
|
export const StateProvider = ({children}) => {
|
|
|
|
return (
|
|
<StateContext.Provider value={useReducer(reducer, initialState)}>
|
|
{children}
|
|
</StateContext.Provider>
|
|
);
|
|
}
|
|
|
|
export const useStateValue = () => useContext(StateContext);
|