Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Savy011/fullstackopen-exercises. My Exercise Submissions for FullStack Open fullstackopen.com/en
Something went wrong. Try again.
856 B · 38 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839import { createContext, useContext, useReducer } from 'react'
const userReducer = (state, action) => { switch (action.type) { case 'SET_USER': return action.payload case 'CLEAR_USER': return null default: return state }}
const UserContext = createContext()
export const UserContextProvider = props => { const [user, userDispatch] = useReducer(userReducer, null)
return ( <UserContext.Provider value={[user, userDispatch]}> {props.children} </UserContext.Provider> )}
export const useUserValue = () => { const userAndDispatch = useContext(UserContext)
return userAndDispatch[0]}
export const useUserDispatch = () => { const userAndDispatch = useContext(UserContext)
return userAndDispatch[1]}
export default UserContext