Created
March 4, 2022 11:30
-
-
Save oguzkaracar/5b3fa3fc4f0e526b5bbf16d57697c015 to your computer and use it in GitHub Desktop.
React Context example usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createContext } from 'react'; | |
import { useContext } from 'react'; | |
// local imports | |
import routes from './fuse-configs/routesConfig'; | |
/* Create Context */ | |
const AppContext = createContext({}); | |
/* Context Provider */ | |
const AppContextProvider = (props) => { | |
return <AppContext.Provider value={{ routes }} {...props} />; | |
}; | |
// AppContext Consumer Hook | |
const useAppContext = () => { | |
const context = useContext(AppContext); | |
if (!context) throw Error('useAppContext must be used within a AppContextProvider'); | |
return context; | |
}; | |
export { AppContext, AppContextProvider, useAppContext }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment