为什么我不能使用 react.createContext()
Why can't i use react.createContext()
嗨,所以我一直在尝试将 React 上下文 api 实施到我的项目中,按照指南中的步骤操作后:https://developerhandbook.com/react/build-a-complete-property-listings-page-with-react/
当我试图显示一些信息时,我最终遇到了一个错误。
这是我在控制台日志中得到的错误:
OrderListProvider.js:6 Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_0_react__.createContext is not a function
at Object../src/context/OrderListProvider.js (OrderListProvider.js:6)
at __webpack_require__ (bootstrap 39db4eed0e38b5656c68:678)
at fn (bootstrap 39db4eed0e38b5656c68:88)
at Object../src/components/Home.js (FetchData.js:3)
at __webpack_require__ (bootstrap 39db4eed0e38b5656c68:678)
at fn (bootstrap 39db4eed0e38b5656c68:88)
at Object../src/App.js (fetch.js:461)
at __webpack_require__ (bootstrap 39db4eed0e38b5656c68:678)
at fn (bootstrap 39db4eed0e38b5656c68:88)
at Object../src/index.js (index.css?f255:26)
at __webpack_require__ (bootstrap 39db4eed0e38b5656c68:678)
at fn (bootstrap 39db4eed0e38b5656c68:88)
at Object.0 (registerServiceWorker.js:108)
at __webpack_require__ (bootstrap 39db4eed0e38b5656c68:678)
at bootstrap 39db4eed0e38b5656c68:724
at bootstrap 39db4eed0e38b5656c68:724
我尝试更新我的反应,因为指南提到我至少需要 16.3,而我只有 16.0.0。
然而,在更新到最新版本后,它仍然说同样的话。我似乎无法在 github.
的发行说明中找到有关它的任何信息
这是我的 createContext 函数:
const OrderListContext = React.createContext(DefaultState);
这是我消费它的地方:
<OrderListProvider>
<OrderListConsumer>
{function (value) {
const { orderList } = value
return (
<ul>
{orderList.map(listing => (
<li>{listing.varetitle}</li>
))}
</ul>
)
}}
</OrderListConsumer>
</OrderListProvider>
编辑:这是我的 package.json
{
"name": "React_Demo",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.3.1",
"react": "^16.10.2",
"react-bootstrap": "^0.32.4",
"react-dom": "^16.10.2",
"react-router-bootstrap": "^0.25.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.2.0",
"rimraf": "^3.0.0"
},
"scripts": {
"start": "rimraf ./build && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
edit2:我在 stackblitz 中重新创建了我的代码,没有从 fetch funktion 获取数据:https://stackblitz.com/edit/react-ssqc4t 它似乎没有显示错误所以我不明白为什么我的代码在 visual studio 2017
edit3:我也尝试过验证并清除我的 npm 缓存,但它仍然显示相同的错误。
edit4: Node.js也已经更新到最新版本了。还是一样的错误。
您必须将 react
& react-dom
的版本升级到 v16.4 或更高版本。
npm update react react-dom
如果你用的是纱线,你可以运行
yarn upgrade react react-dom
所以我设法让它工作,方法是删除我的整个节点模块文件夹并使用
重新安装它
npm install
导出 Context const,它将按如下方式工作:
export const OrderListContext = React.createContext(DefaultState);
嗨,所以我一直在尝试将 React 上下文 api 实施到我的项目中,按照指南中的步骤操作后:https://developerhandbook.com/react/build-a-complete-property-listings-page-with-react/
当我试图显示一些信息时,我最终遇到了一个错误。
这是我在控制台日志中得到的错误:
OrderListProvider.js:6 Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_0_react__.createContext is not a function
at Object../src/context/OrderListProvider.js (OrderListProvider.js:6)
at __webpack_require__ (bootstrap 39db4eed0e38b5656c68:678)
at fn (bootstrap 39db4eed0e38b5656c68:88)
at Object../src/components/Home.js (FetchData.js:3)
at __webpack_require__ (bootstrap 39db4eed0e38b5656c68:678)
at fn (bootstrap 39db4eed0e38b5656c68:88)
at Object../src/App.js (fetch.js:461)
at __webpack_require__ (bootstrap 39db4eed0e38b5656c68:678)
at fn (bootstrap 39db4eed0e38b5656c68:88)
at Object../src/index.js (index.css?f255:26)
at __webpack_require__ (bootstrap 39db4eed0e38b5656c68:678)
at fn (bootstrap 39db4eed0e38b5656c68:88)
at Object.0 (registerServiceWorker.js:108)
at __webpack_require__ (bootstrap 39db4eed0e38b5656c68:678)
at bootstrap 39db4eed0e38b5656c68:724
at bootstrap 39db4eed0e38b5656c68:724
我尝试更新我的反应,因为指南提到我至少需要 16.3,而我只有 16.0.0。
然而,在更新到最新版本后,它仍然说同样的话。我似乎无法在 github.
的发行说明中找到有关它的任何信息这是我的 createContext 函数:
const OrderListContext = React.createContext(DefaultState);
这是我消费它的地方:
<OrderListProvider>
<OrderListConsumer>
{function (value) {
const { orderList } = value
return (
<ul>
{orderList.map(listing => (
<li>{listing.varetitle}</li>
))}
</ul>
)
}}
</OrderListConsumer>
</OrderListProvider>
编辑:这是我的 package.json
{
"name": "React_Demo",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.3.1",
"react": "^16.10.2",
"react-bootstrap": "^0.32.4",
"react-dom": "^16.10.2",
"react-router-bootstrap": "^0.25.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.2.0",
"rimraf": "^3.0.0"
},
"scripts": {
"start": "rimraf ./build && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
edit2:我在 stackblitz 中重新创建了我的代码,没有从 fetch funktion 获取数据:https://stackblitz.com/edit/react-ssqc4t 它似乎没有显示错误所以我不明白为什么我的代码在 visual studio 2017
edit3:我也尝试过验证并清除我的 npm 缓存,但它仍然显示相同的错误。
edit4: Node.js也已经更新到最新版本了。还是一样的错误。
您必须将 react
& react-dom
的版本升级到 v16.4 或更高版本。
npm update react react-dom
如果你用的是纱线,你可以运行
yarn upgrade react react-dom
所以我设法让它工作,方法是删除我的整个节点模块文件夹并使用
重新安装它npm install
导出 Context const,它将按如下方式工作:
export const OrderListContext = React.createContext(DefaultState);