Skip to content

Instantly share code, notes, and snippets.

@destinio
Last active February 27, 2021 22:38
Show Gist options
  • Save destinio/fec30a88fc136c7e3c4a182d4a60d421 to your computer and use it in GitHub Desktop.
Save destinio/fec30a88fc136c7e3c4a182d4a60d421 to your computer and use it in GitHub Desktop.
AppContext
import React, { createContext, useContext, useReducer} from 'react'
const AppContext = createContext()
export function useAppContext() {
return useContext(AppContext)
}
const initState = {
users: []
}
function appReducer(state, action) {
switch (action.type) {
default:
return state
}
}
export default function AppContextProvider({children}) {
const [state, dispatch] = useReducer(appReducer, initState)
return <AppContext.Provider value={{ state, dispatch }}>{children}</AppContext.Provider>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment