“React.createContext 不是函数” - 但我没有使用它
Posted
技术标签:
【中文标题】“React.createContext 不是函数” - 但我没有使用它【英文标题】:"React.createContext is not a function" - but I am not using it 【发布时间】:2019-06-15 14:18:25 【问题描述】:我在运行 react-native 应用程序时收到“React.createContext 不是函数”错误。我在代码中搜索了“createContent”,它不存在。
知道错误的可能原因是什么。
这是一个已经在安卓上运行的应用的 ios 版本。
这是我的 package.json 文件:
"name": "newapp",
"version": "0.0.2",
"private": true,
"scripts":
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest --verbose --coverage",
"test:update": "jest --verbose --coverage --updateSnapshot",
"test:watch": "jest --verbose --watch",
"coverage": "jest --verbose --coverage && xdg-open ./coverage/lcov-report/index.html",
"lint": "npx eslint --fix ./src/*"
,
"dependencies":
"apollo-boost": "0.1.17",
"axios": "^0.18.0",
"babel": "^6.23.0",
"buffer": "5.1.0",
"cross-fetch": "^3.0.0",
"enzyme": "^3.3.0",
"graphql": "0.13.2",
"graphql-tag": "2.10.0",
"jasmine-react-helpers": "^0.2.2",
"lodash": "4.17.5",
"moment": "2.21.0",
"query-string": "^6.1.0",
"react": "16.2.0",
"react-addons-test-utils": "^15.6.2",
"react-apollo": "2.2.4",
"react-dom": "^16.4.0",
"react-native": "0.51.0",
"react-native-elements": "0.19.0",
"react-native-fabric": "^0.5.1",
"react-native-htmlview": "^0.12.1",
"react-native-link-preview": "^1.3.5",
"react-native-login": "^0.0.1-alpha.2",
"react-native-login-keycloak": "^1.0.2",
"react-native-onesignal": "3.2.6",
"react-native-push-notification": "https://github.com/Dhanraj-bidchat/react-native-push-notification.git",
"react-native-sleek-loading-indicator": "^0.1.3",
"react-native-spinkit": "^1.1.1",
"react-native-svg": "6.2.2",
"react-native-swipe-cards": "^0.1.1",
"react-native-swiper": "1.5.13",
"react-native-vector-icons": "4.5.0",
"react-navigation": "1.5.6",
"react-redux": "5.0.7",
"redux": "^3.7.2",
"redux-logger": "^3.0.6",
"redux-persist": "5.9.1",
"redux-thunk": "^2.2.0",
"victory-native": "0.17.2",
"whatwg-fetch": "2.0.4"
,
"devDependencies":
"babel-eslint": "^8.2.3",
"babel-jest": "^22.4.4",
"babel-plugin-dynamic-import-node": "^1.2.0",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
"babel-preset-env": "^1.7.0",
"babel-preset-react-native": "4.0.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-to-json": "^3.3.4",
"eslint": "^4.19.1",
"eslint-config-standard": "^11.0.0",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-jest": "^21.17.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-only-warn": "^1.0.1",
"eslint-plugin-promise": "^3.8.0",
"eslint-plugin-react": "^7.9.1",
"eslint-plugin-standard": "^3.1.0",
"jest": "^23.5.0",
"jest-resolve": "^23.0.0",
"jest-serializer-enzyme": "^1.0.0",
"react-native-mock-render": "^0.0.26",
"react-test-renderer": "^16.3.2",
"redux-mock-store": "^1.5.1",
"sinon": "^5.0.10"
,
"jest":
"preset": "react-native",
"setupFiles": [
"./jest-setup.js"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"modulePaths": [
"<rootDir>/src",
"<rootDir>/node_modules"
],
"transform":
"^.+\\.js$": "babel-jest",
"^.+\\.jsx?$": "babel-jest"
,
"transformIgnorePatterns": [
"node_modules/(?!(react-native-safe-area-view|react-navigation|react-native-htmlview|react-native-fabric|react-native-login|react-native-elements|react-native-vector-icons|react-native-spinkit|victory-pie|victory-chart|victory-core|react-native-svg|react-native|redux-persist|victory-native|react-native-swipe-cards|react-native-swiper)/)"
],
"moduleNameMapper":
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/assetsTransformer.js",
"\\.(css|less)$": "<rootDir>/assetsTransformer.js"
【问题讨论】:
【参考方案1】:添加@emotion/react
会导致类似的错误TypeError: React.createContext is not a function
。在我删除 @emotion/react
并将 @emotion/styled
从 11 降级到 10 后,错误消失了。
【讨论】:
【参考方案2】:在您的包中 react
和 react-dom
没有相同的版本,这就是您收到错误的原因。 react-dom@16.4.0
想要访问 React.createContext
但 react@16.2.0 不会。
为了让它工作,运行以下命令:
yarn upgrade react@16.4.0
或者如果你使用 NPM:
npm install react@16.4.0
【讨论】:
我在我的 react native 0.53 上尝试了上述修复,它在 react 16.2 上出现了同样的错误,但我又收到了另一个关于styled-components.native.cjs.js'
的错误
我有相同版本的 react 和 react-dom 并不能解决问题,并且问题 TypeError: _react.default.createContext is not a function 仍然存在。
【参考方案3】:
如果 npm i react@latest react-dom@latest
不起作用,请尝试执行 npm update
,它将更新所有包。
【讨论】:
【参考方案4】:使用 npm 我通过升级到 16.4.1 解决了这个问题:
npm i react@16.4.1
【讨论】:
【参考方案5】:尝试将 React 升级到 React 到 16.4.1
【讨论】:
升级可能会很痛苦。我会等待其他答复。谢谢! 您想发表评论并将其作为答案发布吗? 没有。我记得有这个问题。升级到 16.4.1 为我解决了这个问题。以上是关于“React.createContext 不是函数” - 但我没有使用它的主要内容,如果未能解决你的问题,请参考以下文章