Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未捕获类型错误:\网页包\导入的\模块\ 0\反应\默认值。a.createContext不是函数_Javascript_Node.js_Reactjs_Webpack - Fatal编程技术网

Javascript 未捕获类型错误:\网页包\导入的\模块\ 0\反应\默认值。a.createContext不是函数

Javascript 未捕获类型错误:\网页包\导入的\模块\ 0\反应\默认值。a.createContext不是函数,javascript,node.js,reactjs,webpack,Javascript,Node.js,Reactjs,Webpack,我以以下方式使用@loadable/component: app.js import React from 'react'; import loadable from "@loadable/component"; // ... const Welcome = loadable(() => import(/* webpackChunkName: "welcome" */ "./components/Welcome") );

我以以下方式使用
@loadable/component

app.js

import React from 'react';
import loadable from "@loadable/component";

// ...

const Welcome = loadable(() =>
  import(/* webpackChunkName: "welcome" */ "./components/Welcome")
);

// ...

let root = document.getElementById('root');
if (root) {

    render(
    <BrowserRouter>
      <Switch>
        ...
        <Route path="/welcome" component={Welcome} />
        ...
      </Switch>
    </BrowserRouter>,
    root
  );
}
然后,在浏览器上打开应用程序时,在Chrome控制台上,我收到以下错误:

app.js?v=:23402 Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_0_react___default.a.createContext is not a function
    at Object.553 (app.js?v=:23402)
    at __webpack_require__ (manifest.js?v=:55)
    at Object.403 (app.js?v=:21450)
    at __webpack_require__ (manifest.js?v=:55)
    at Object.402 (app.js?v=:21415)
    at __webpack_require__ (manifest.js?v=:55)
    at webpackJsonpCallback (manifest.js?v=:26)
    at app.js?v=:1
当点击上面的:
app.js?v=:23402
时,我得到以下信息:

$ npm run watch

> cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=./webpack.config.js

 10% building modules 2/2 modules 0 active
Webpack is watching the files…

 95% emitting DONE  Compiled successfully in 3775ms5:40:45 AM

                        Asset       Size  Chunks                    Chunk Names
welcome.js?v=##BUILD_NUMBER##    15.9 kB       0  [emitted]         welcome
                   /vendor.js    2.49 MB       1  [emitted]  [big]  /vendor
                      /app.js     1.9 MB       2  [emitted]  [big]  /app
                 /manifest.js    5.83 kB       3  [emitted]         /manifest
            ../css/styles.css    59.5 kB       2  [emitted]         /app

这是
package.json的一个片段:

{
  "private": true,
  "scripts": {
      ...
  },
  "devDependencies": {
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.23.0",
    "babel-preset-stage-2": "^6.24.1",
    "bootstrap-less": "^3.3.8",
    "cross-env": "^5.0.1",
    "flux": "^3.1.3",
    "laravel-mix": "^2.1",
    "laravel-mix-purgecss": "^5.0.0-rc.1",
    "less": "^2.7.2",
    "less-loader": "^4.0.5",
    "lodash": "^4.17.4",
    "webpack": "^3.5.0"
  },
  "dependencies": {
    "@loadable/component": "^5.14.1",
    "axios": "^0.16.2",
    "classnames": "^2.2.6",
    "dotenv": "^8.2.0",
    "jquery": "^3.2.1",
    "md5": "^2.2.1",
    "moment": "^2.19.1",
    "npm": "^6.1.0",
    "query-string": "^6.4.2",
    "react": "^15.6.1",
    "react-bootstrap": "^0.31.3",
    "react-collapsible": "^2.2.0",
    "react-cookies": "^0.1.0",
    "react-dom": "^15.6.1",
    "react-number-format": "^2.0.4",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "react-slick": "^0.15.4",
    "react-text-mask": "^5.0.2",
    "store2": "^2.5.5"
  },
  "main": "webpack.mix.js"
}
有没有办法解决这个问题

注意:在使用
@loadable/component
之前,一切都很正常,但我需要使用:
@loadable/component


谢谢

出现此错误的原因是您的一个依赖项(甚至可能是您的代码)依赖于
React.createContext
方法

这是16.3版中引入的“上下文API”的一部分

您必须删除需要此更高版本的react的依赖项,或者将react版本升级到至少16.3

通常,当包.json中的依赖项依赖于react的特定版本时,它会将该版本列为“对等依赖项”。如果是这样,当运行
npm install
时,您将收到如下警告

npm WARN somedependency@a.b.c requires a peer of react@^16.3.0 but...
如果需要,可以使用此警告查找罪犯包。如果您没有收到这样的警告,那么可能是有问题的代码在您的代码库中,或者某些依赖项错误地声明了它们的对等项

例如,使用您在帖子中包含的软件包版本运行
npm install
后,我们会收到以下警告(不包括本问题范围内的弃用警告):

npm WARN @loadable/component@5.14.1 requires a peer of react@>=16.3.0 but none is installed. You must install peer dependencies yourself.

作为一种解决方法,您有时还可以检查问题包的早期版本是否与早期的对等依赖项版本兼容:)

我不确定,但如果您将
导入从“React”更改为“React”导入到
导入*作为“反应”的反应这会改变什么吗?另外,你能不能包括你正在使用的react的版本?更改后
从“react”导入react导入到
导入*作为“反应”的反应我也有同样的错误。你完全正确。我必须更新:从:“react”:“^15.6.1”、“react-dom”:“^15.6.1”、“react-router”:“^4.2.0”、“react-router-dom”:“^4.2.2”到:“react”:“^16.3”、“react-dom”:“^16.3”、“react-router”:“^5.0.1”、“react-router-dom”:“^5.0.1”,