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

這里有個(gè)例子來(lái)展示錯(cuò)誤是如何發(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;
上述代碼片段的問(wèn)題在于,文件的擴(kuò)展名為.ts ,但是我們?cè)诶锩婢帉慗SX代碼。
tsx
這是不被允許的,因?yàn)闉榱四茉赥ypeScript文件中使用JSX,我們必須這樣做:
- 以
.tsx擴(kuò)展名命名文件 - 在
tsconfig.json文件中開(kāi)啟jsx選項(xiàng)
確保所有你編寫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后,如果問(wèn)題仍未解決,請(qǐng)嘗試重啟你的IDE和開(kāi)發(fā)服務(wù)器。
打開(kāi)tsconfig.json文件,并確保jsx選項(xiàng)被設(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選項(xiàng)被設(shè)置為react-jsx時(shí),它會(huì)導(dǎo)致編譯器拋出.js文件,其中的JSX被改為_jsx調(diào)用。
如有必要請(qǐng)重啟你的IDE和開(kāi)發(fā)服務(wù)器。你的開(kāi)發(fā)服務(wù)器不會(huì)接收這些變化,直到你停止它并重新運(yùn)行npm start命令。
安裝@types/包
在React中出現(xiàn)"Cannot find namespace context"錯(cuò)誤的另一個(gè)原因是,我們沒(méi)有安裝必要的@types/包。
在項(xiàng)目的根路徑下打開(kāi)終端,并運(yùn)行以下命令:
# ??? 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包。
如果仍然報(bào)錯(cuò),嘗試刪除node_modules和package-lock.json文件(不是package.json),重新運(yùn)行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
如果錯(cuò)誤仍然存在,請(qǐng)確保重新啟動(dòng)你的IDE和開(kāi)發(fā)服務(wù)器。VSCode經(jīng)常出現(xiàn)故障,重啟有時(shí)會(huì)解決一些問(wèn)題。
手動(dòng)添加
如果你仍然得到"Cannot find namespace Context"的錯(cuò)誤,打開(kāi)你的package.json文件,確保它在devDependencies對(duì)象中包含以下包。
// 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"
}
}
你可以嘗試手動(dòng)添加上述依賴,并重新運(yùn)行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報(bào)錯(cuò)Cannot find namespace context的詳細(xì)內(nèi)容,更多關(guān)于React報(bào)錯(cuò)解決的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 解決React?hook?'useState'?cannot?be?called?in?a?class?component報(bào)錯(cuò)
- 解決React報(bào)錯(cuò)You provided a `checked` prop to a form field
- 解決React報(bào)錯(cuò)Invalid hook call
- 解決React報(bào)錯(cuò)Property does not exist on type 'JSX.IntrinsicElements'
- 解決React報(bào)錯(cuò)Rendered more hooks than during the previous render
- 解決React報(bào)錯(cuò)Parameter 'props' implicitly has an 'any' type
- 解決React報(bào)錯(cuò)React?Hook?useEffect?has?a?missing?dependency
- React hook 'useState' is called conditionally報(bào)錯(cuò)解決
相關(guān)文章
React Native之prop-types進(jìn)行屬性確認(rèn)詳解
本篇文章主要介紹了React Native之prop-types進(jìn)行屬性確認(rèn)詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
使用React-Router實(shí)現(xiàn)前端路由鑒權(quán)的示例代碼
這篇文章主要介紹了使用React-Router實(shí)現(xiàn)前端路由鑒權(quán)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
最新版React?Native環(huán)境搭建(親測(cè))
這篇文章主要介紹了最新版React?Native環(huán)境搭建,React Native的運(yùn)行需要依賴原生Android和iOS環(huán)境,因此需要分別安裝原生Android和iOS的開(kāi)發(fā)環(huán)境,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
React使用useImperativeHandle自定義暴露給父組件的示例詳解
useImperativeHandle?是?React?提供的一個(gè)自定義?Hook,用于在函數(shù)組件中顯式地暴露給父組件特定實(shí)例的方法,本文將介紹?useImperativeHandle的基本用法、常見(jiàn)應(yīng)用場(chǎng)景,需要的可以參考下2024-03-03
React styled-components設(shè)置組件屬性的方法
這篇文章主要介紹了styled-components設(shè)置組件屬性的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

