This commit is contained in:
Elijah Lucian
2021-04-14 18:16:50 -06:00
parent 8df1fed14c
commit 1904bb8668
8 changed files with 59 additions and 91 deletions
+28
View File
@@ -0,0 +1,28 @@
import React, { createContext, useContext } from 'react'
import { Api } from '../api'
type Props = {
children: React.ReactNode
api: Api
}
type Context = {
api: Api
}
// Just find-replace "AppContext" with whatever context name you like. (ie. DankContext)
const AppContext = createContext<Context | null>(null)
export const AppContextProvider = ({ api, children }: Props) => {
return <AppContext.Provider value={{ api }}>{children}</AppContext.Provider>
}
export const useAppContext = () => {
const context = useContext(AppContext)
if (!context)
throw new Error(
'AppContext must be called from within the AppContextProvider',
)
return context
}