I'm trying to toggle element by clicking on button, however it doesn't do anything. Also note that code is simplified as I didn't want to copy whole app, I just included parts I thought are important. Here is code:
Navbar.js ..Here I try to invoke the function it comes from context.js file where I setup my contextAPI
import { useGlobalContext } from "./context"; const { openSidebar, openSubmenu, closeSubmenu } = useGlobalContext(); <button className="btn toggle-btn" onClick={openSidebar}>context.js
const AppContext = React.createContext(); const AppProvider = ({ children }) => { const [isSidebarOpen, setIsSidebarOpen] = useState(false); const openSidebar = () => { console.log("test") setIsSidebarOpen(true); }; return ( <AppContext.Provider value={(isSidebarOpen, openSidebar, closeSidebar)}> {children} </AppContext.Provider> ); } export const useGlobalContext = () => { return useContext(AppContext); }; export { AppContext, AppProvider };App.js
document.addEventListener("click", (e) => { console.log(e); console.log(e.target); });index.js
import App from "./App"; import { AppProvider } from "./context"; ReactDOM.render( <React.StrictMode> <AppProvider> <App /> </AppProvider> </React.StrictMode>, document.getElementById("root") );
The event listener from App.js displays correctly event and also event.target which is the button. However I don't get any log to console from context.js where it should log "test", not error, not warning, nothing... onClick is completely ignored.