【发布时间】:2020-02-18 08:36:09
【问题描述】:
您好,我一直在尝试在我的项目中实现反应上下文 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 也已更新到最新版本。还是一样的错误。
【问题讨论】:
-
你
import React from 'react'了吗?安装新版本后是否重启了 webpack 开发服务器? -
const OrderListContext = React.createContext(DefaultState)是的,我包括了 ``` import * as React from 'react ``` -
有什么特殊的方法可以重启webpack dev server吗?我刚刚从 Visual Studio 推出了 iis express。
-
停止并重新启动...或重新启动 Visual Studio ¯\_(ツ)_/¯
标签: node.js reactjs react-context