Skip to content

Instantly share code, notes, and snippets.

@oguzkaracar
Created March 4, 2022 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oguzkaracar/5b3fa3fc4f0e526b5bbf16d57697c015 to your computer and use it in GitHub Desktop.
Save oguzkaracar/5b3fa3fc4f0e526b5bbf16d57697c015 to your computer and use it in GitHub Desktop.
React Context example usage
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