Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find the context Next Js [closed]


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

like image 788
PrinceLoren Avatar asked Oct 29 '25 05:10

PrinceLoren


1 Answers

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

like image 120
Dhilip H Avatar answered Oct 30 '25 23:10

Dhilip H