import { createContext, ReactNode, useState } from 'react';
import { MenuItem } from '../interfaces/menu.interfaces';
import { TopLevelCategory } from '../interfaces/page.interface';
export interface IAppContext {
menu: MenuItem[];
firstCategory: TopLevelCategory;
setMenu?: (newMenu: MenuItem[]) => void;
}
export const AppContext = createContext<IAppContext>({menu: [], firstCategory: TopLevelCategory.Courses});
export const AppContextProvider = ({ menu, firstCategory, children }: IAppContext & { children: ReactNode }): JSX.Element => {
const [menuState, setMenuState] = useState<MenuItem[]>(menu);
const setMenu = (newMenu: MenuItem[]) => {
setMenuState(newMenu);
};
return <AppContext.Provider value={{ menu: menuState, firstCategory, setMenu }}>
{children}
</AppContext.Provider>;
};
I have this code, but it does not want to see the AppContext and in the error shows Cannot find namespace 'AppContext'. ts(2503)
I don't understand why it can't find the AppContext when I create it above, help solve the problem plz
here
Use .tsx instead of .ts. That should fix the problem.
If you need more information, please refer - Cannot find namespace 'ctx' error when creating Context with react - typescript
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With