0

I am trying to use useContext hook, my context and provider file looks like this:

import { createContext, useState, ReactNode, useContext } from 'react';

interface SideNavContextType {
  isOpen: boolean;
  toggleSideNav: () => void;
}

export const SideNavContext = createContext<SideNavContextType>({
  isOpen: false,
  toggleSideNav: () => {},
});

export const useSideNav = () => {
  return useContext(SideNavContext);
};

export const SideNavProvider = ({ children }: { children: ReactNode }) => {
  const [isOpen, setIsOpen] = useState(false);

  const toggleSideNav = () => {
    setIsOpen(!isOpen);
  };

  const value = {
    isOpen,
    toggleSideNav,
  };

  return (
    <SideNavContext.Provider value={value}>{children}</SideNavContext.Provider>
  );
};

I'm getting an error with this line :

 <SideNavContext.Provider value={value}>{children}</SideNavContext.Provider>

the error is Cannot find namespace 'SideNavContext'. I have no clue why this is happening....

1

1 Answer 1

0

I needed to change my file name to end with tsx for this to work...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.