【问题标题】:Regarding Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead关于警告:不推荐通过主 React 包访问 PropTypes。改用 npm 的 prop-types 包
【发布时间】:2017-10-02 22:12:51
【问题描述】:

我正在用酶开玩笑,但我收到了上述警告,具体来自我要测试的类中的 react-router 依赖性。我的问题是为什么我收到这个警告,因为我已经将我的反应版本降级到 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)

我不想从 Router v2 升级到最新版本 4,因为我必须在我的代码中进行大量升级。为什么会出现这个错误,尽管我没有使用最新版本的 React,其中这个包从“react”移动到“prop-types”。

【问题讨论】:

    标签: reactjs npm react-router jestjs enzyme


    【解决方案1】:

    从 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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我将 ^ 放在所有包裹的前面。我是否应该将其替换为 ~ 以使自动升级更具选择性。
      【解决方案3】:

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

      版本 2

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

      版本 3

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

      (尽管您可以安全地使用3.0.5

      【讨论】:

        猜你喜欢
        • 2017-09-04
        • 1970-01-01
        • 2021-10-15
        • 2017-03-01
        • 2016-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多