关于警告:不推荐通过主 React 包访问 PropTypes。改用 npm 中的 prop-types 包

Regarding Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead

我 运行 和酶开玩笑,但我收到上述警告,特别是来自 class 我正在尝试测试的 react-router 依赖项。我的问题是为什么我会收到此警告,因为我已经将我的 React 版本降级到 15.1.1。

Package.json
 "dependencies": {
    "axios": "^0.16.1",
    "babel": "^6.23.0",
    "babel-polyfill": "^6.23.0",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.5.0",
    "bootstrap": "^3.3.5",
    "boron": "^0.2.3",
    "browserify": "^13.0.1",
    "connect-history-api-fallback": "^1.2.0",
    "debounce": "^1.0.0",
    "envify": "^3.4.0",
    "es6-promise": "^3.2.1",
    "font-awesome": "^4.6.3",
    "jquery": "^2.2.4",
    "jwt-decode": "^2.2.0",
    "minimist": "^1.2.0",
    "node-fetch": "1.6.3",
    "node-notifier": "^4.6.0",
    "object-assign": "^4.1.0",
    "react": "^15.1.0",
    "react-autosuggest": "^3.8.0",
    "react-dnd": "^2.1.4",
    "react-dnd-html5-backend": "^2.1.2",
    "react-document-title": "^2.0.3",
    "react-dom": "^15.1.0",
    "react-dropdown": "^1.0.4",
    "react-quill": "^0.4.1",
    "react-router": "^2.4.1",
    "react-select": "^1.0.0-beta13",
    "react-select2-wrapper": "^0.6.1",
    "react-tag-input": "^3.0.3",
    "reflux": "0.4.1",
    "request": "^2.81.0",
    "request-promise": "^4.2.0",
    "toastr": "^2.1.0",
    "underscore": "^1.8.3",
    "vinyl-source-stream": "^1.1.0",
    "watchify": "^3.7.0",
    "webpack": "^2.4.1",
    "whatwg-fetch": "^1.0.0"
  },
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-jest": "^6.0.1",
    "babel-loader": "^7.0.0",
    "babelify": "^7.3.0",
    "css-loader": "^0.28.0",
    "del": "^2.2.0",
    "enzyme": "^2.1.0",
    "eslint": "^3.19.0",
    "eslint-config-defaults": "^9.0.0",
    "eslint-loader": "^1.7.1",
    "eslint-plugin-react": "^6.10.3",
    "extract-text-webpack-plugin": "^2.1.0",
    "file-loader": "^0.11.1",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^15.1.1",
    "jest-cli": "^15.1.1",
    "less": "^2.7.2",
    "less-loader": "^4.0.3",
    "node-sass": "^4.5.2",
    "react-addons-test-utils": "^15.1.1",
    "react-bootstrap": "^0.29.5",
    "regenerator-runtime": "^0.10.5",
    "sass-loader": "^6.0.3",
    "scss-loader": "0.0.1",
    "url-loader": "^0.5.8",
    "yargs": "^7.1.0"
  },

错误的完整堆栈跟踪是:

console.error node_modules\fbjs\lib\warning.js:36
    Warning: ReactTestUtils has been moved to react-dom/test-utils. Update references to remove this warning.

 FAIL  web\test\testClass.test.js
  ● Test suite failed to run

    Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.

      at CustomConsole.Object.<anonymous>.console.error (web\test\jestSetup.js:12:11)
      at printWarning (node_modules\fbjs\lib\warning.js:36:17)
      at warning (node_modules\fbjs\lib\warning.js:60:22)
      at Object.get [as PropTypes] (node_modules\react\lib\React.js:95:49)
      at Object.<anonymous> (node_modules\react-router\lib\InternalPropTypes.js:9:18)
      at Object.<anonymous> (node_modules\react-router\lib\PropTypes.js:12:26)
      at Object.<anonymous> (node_modules\react-router\lib\index.js:15:19)
      at Object.<anonymous> (web\js\components\common\toolBar.jsx:2:20)
      at Object.<anonymous> (web\test\testClass.test.js:5:16)
      at process._tickCallback (internal\process\next_tick.js:103:7)

我不想从路由器 v2 升级到最新版本 4,因为我必须在我的代码中进行大量升级。尽管我没有使用最新版本的 React,但此包已从 'react' 移动到 'prop-types'。

为什么会出现此错误

如果您降级了 React 节点模块,请务必删除 node_modules 目录并重新安装它们。很可能您的其他依赖项之一也已升级。由于您没有固定您的版本,您可能最终会在同一个地方。我会固定你的版本或使用 yarn 生成一个 yarn.lock 文件。

从 React 15.5.0 开始,PropTypes 不再是 react 包的一部分,现在是它自己的独立包。所以如果你想摆脱警告你需要 npm install prop-types 然后 import PropTypes from 'prop-types'

https://facebook.github.io/react/blog/2017/04/07/react-v15.5.0.html#migrating-from-react.proptypes

TestUtils 也一样: https://facebook.github.io/react/blog/2017/04/07/react-v15.5.0.html#react-test-utils

React Router 已更新包以处理 'createClass' 和 PropTypes 的 React 库更改。

版本 2

他们好像没有更新这个。有道理,因为版本 4 是最新的。

版本 3

https://github.com/ReactTraining/react-router/releases/tag/v3.0.4

(尽管您可能会安全地使用 3.0.5