解決React報錯Cannot?find?namespace?context
總覽
在React中,為了解決"Cannot find namespace context"錯誤,在你使用JSX的文件中使用.tsx擴(kuò)展名,在你的tsconfig.json文件中把jsx設(shè)置為react-jsx,并確保為你的應(yīng)用程序安裝所有必要的@types包。

這里有個例子來展示錯誤是如何發(fā)生的。
// App.ts
import React from 'react';
interface UserCtx {
first: string;
last: string;
age: number;
}
const AuthContext = React.createContext<UserCtx | null>(null);
const authContext: UserCtx = {
first: 'James',
last: 'Doe',
age: 30,
};
const App = () => {
// ?? Cannot find namespace 'AuthContext'.ts(2503)
return (
<AuthContext.Provider value={authContext}>
<h2>Your app here</h2>
</AuthContext.Provider>
);
};
export default App;
上述代碼片段的問題在于,文件的擴(kuò)展名為.ts ,但是我們在里面編寫JSX代碼。
tsx
這是不被允許的,因為為了能在TypeScript文件中使用JSX,我們必須這樣做:
- 以
.tsx擴(kuò)展名命名文件 - 在
tsconfig.json文件中開啟jsx選項
確保所有你編寫JSX代碼的文件都有.tsx擴(kuò)展名。
// App.tsx
import React from 'react';
interface UserCtx {
first: string;
last: string;
age: number;
}
const AuthContext = React.createContext<UserCtx | null>(null);
const authContext: UserCtx = {
first: 'James',
last: 'Doe',
age: 30,
};
const App = () => {
return (
<AuthContext.Provider value={authContext}>
<h2>Your app here</h2>
</AuthContext.Provider>
);
};
export default App;
更新完文件的擴(kuò)展名為.tsx后,如果問題仍未解決,請嘗試重啟你的IDE和開發(fā)服務(wù)器。
打開tsconfig.json文件,并確保jsx選項被設(shè)置為react-jsx。
// tsconfig.json
{
"compilerOptions": {
"jsx": "react-jsx", // ??? make sure you have this
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": ["src/**/*"]
}
當(dāng)jsx選項被設(shè)置為react-jsx時,它會導(dǎo)致編譯器拋出.js文件,其中的JSX被改為_jsx調(diào)用。
如有必要請重啟你的IDE和開發(fā)服務(wù)器。你的開發(fā)服務(wù)器不會接收這些變化,直到你停止它并重新運行npm start命令。
安裝@types/包
在React中出現(xiàn)"Cannot find namespace context"錯誤的另一個原因是,我們沒有安裝必要的@types/包。
在項目的根路徑下打開終端,并運行以下命令:
# ??? with NPM npm install --save-dev @types/react @types/react-dom @types/node @types/jest typescript # ------------------------------------------------------ # ??? with YARN yarn add @types/react @types/react-dom @types/node @types/jest typescript --dev
該命令為react,react-dom,node,jest安裝類型聲明文件,并安裝typescript包。
如果仍然報錯,嘗試刪除node_modules和package-lock.json文件(不是package.json),重新運行npm install并重啟你的IDE。
# ??? delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ??? clean npm cache npm cache clean --force npm install
如果錯誤仍然存在,請確保重新啟動你的IDE和開發(fā)服務(wù)器。VSCode經(jīng)常出現(xiàn)故障,重啟有時會解決一些問題。
手動添加
如果你仍然得到"Cannot find namespace Context"的錯誤,打開你的package.json文件,確保它在devDependencies對象中包含以下包。
// package.json
{
// ... rest
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/node": "^17.0.24",
"@types/react": "^18.0.5",
"@types/react-dom": "^18.0.1",
"typescript": "^4.6.3"
}
}
你可以嘗試手動添加上述依賴,并重新運行npm install。
npm install
或者安裝依賴包的最新版本:
# ??? with NPM npm install --save-dev @types/react@latest @types/react-dom@latest @types/node@latest @types/jest@latest typescript@latest # ------------------------------------------------------ # ??? with YARN yarn add @types/react@latest @types/react-dom@latest @types/node@latest @types/jest@latest typescript@latest --dev
原文鏈接:bobbyhadz.com/blog/react-…
以上就是解決React報錯Cannot find namespace context的詳細(xì)內(nèi)容,更多關(guān)于React報錯解決的資料請關(guān)注腳本之家其它相關(guān)文章!
- 解決React?hook?'useState'?cannot?be?called?in?a?class?component報錯
- 解決React報錯You provided a `checked` prop to a form field
- 解決React報錯Invalid hook call
- 解決React報錯Property does not exist on type 'JSX.IntrinsicElements'
- 解決React報錯Rendered more hooks than during the previous render
- 解決React報錯Parameter 'props' implicitly has an 'any' type
- 解決React報錯React?Hook?useEffect?has?a?missing?dependency
- React hook 'useState' is called conditionally報錯解決
相關(guān)文章
圖文示例講解useState與useReducer性能區(qū)別
這篇文章主要為大家介紹了useState與useReducer性能區(qū)別圖文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
React + webpack 環(huán)境配置的方法步驟
本篇文章主要介紹了React + webpack 環(huán)境配置的方法步驟,詳解的介紹了開發(fā)環(huán)境的配置搭建,有興趣的可以了解一下2017-09-09
通過實例學(xué)習(xí)React中事件節(jié)流防抖
這篇文章主要介紹了通過實例學(xué)習(xí)React中事件節(jié)流防抖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,,需要的朋友可以參考下2019-06-06
react-native中路由頁面的跳轉(zhuǎn)與傳參的實例詳解
這篇文章主要介紹了react-native中路由頁面的跳轉(zhuǎn)與傳參,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08

