【问题标题】:Why can't i use react.createContext()为什么我不能使用 react.createContext()
【发布时间】: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


【解决方案1】:

您必须将react & react-dom 的版本升级到v16.4 或更高版本。

npm update react react-dom

如果你使用的是yarn,你可以运行

yarn upgrade react react-dom

工作演示
https://stackblitz.com/edit/react-context-api-456158

【讨论】:

  • 对不起,我应该包含 package.json,但我已将其更新到 16.10.2,它仍然显示相同的错误
  • 我附上了链接,您可以在其中找到工作演示。能否请您比较一下并尝试找出问题所在?
  • 据我所知,唯一的区别是我包含了一个默认状态。我尝试删除它,但它仍然给出了同样的错误
  • 你可以在 stackblitz 上复制同样的问题吗?
  • stackblitz.com/edit/react-ssqc4t 我做的几乎和我的一模一样,只是没有获取数据。我将它添加到默认状态。问题没有显示,所以我不确定发生了什么
【解决方案2】:

所以我设法让它工作,通过删除我的整个节点模块文件夹并再次安装它

npm install

【讨论】:

    猜你喜欢
    • 2010-10-28
    • 2010-10-28
    • 2011-03-26
    • 2022-01-03
    • 2012-07-16
    • 2012-03-13
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多