I'm new to React and I'm doing my best to put together an app, so I've created two main sections and I'm at a point where I would like to combine them, but I'm not exactly sure how to go about it.
Here is the error I'm getting:
TypeError: Cannot read property 'openSidebar' of undefined
and it points to this component:
// Sidebar button and opening of sidebar
// main function created to open sidebar button
import React from 'react';
import { FaBars } from 'react-icons/fa';
import { useGlobalContext } from './context';
const Home = () => { /*const data can setup data form each button under sidebar */ //<----- ERROR HERE
const { openSidebar } = useGlobalContext();
return (
<main>
<button onClick={openSidebar} className='sidebar-toggle'>
<FaBars />
</button>
</main>
);
};
export default Home;
I've searched the error in many ways but they all seemed so specific to the app they occurred on.
Thank you for any help you can provide. Please let me know if there is more information needed!
EDIT:
Below is the component that handles the const opendSidebar
// context class need to be added without change,
// setting of close open sidebar
import React, { useState, useContext } from 'react';
const AppContext = React.createContext(undefined, undefined);
const AppProvider = ({ children }) => {
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
const openSidebar = () => {
setIsSidebarOpen(true);
};
const closeSidebar = () => {
setIsSidebarOpen(false);
};
return (
<AppContext.Provider
value={{
isSidebarOpen,
openSidebar,
closeSidebar,
}}
>
{children}
</AppContext.Provider>
);
};
export const useGlobalContext = () => {
return useContext(AppContext);
};
export { AppContext, AppProvider };