【问题标题】:Still getting PropTypes warning after installing react-proptypes, upgrading code and major upgrades on node modules;在 node 模块上安装 react-proptypes、升级代码和重大升级后仍然收到 PropTypes 警告;
【发布时间】:2018-09-07 08:19:48
【问题描述】:

我正在开发一个 React 项目,并试图摆脱以下关于 PropTypes 的警告。我对此进行了大量阅读,并对所有依赖项进行了重大更新,安装了 react-proptypes 并更新了我的代码,但我仍然收到警告。

即使在从我自己的代码中取出 PropTypes 并改用 this.props.history.push('/') 后,我也会收到相同的警告,所以我不得不认为警告来自我的依赖项之一,但它们都是最新的(除了 redux-form@7.4,我还没有升级,因为它给了我一个类似的 createComponent 警告)。

关于为什么的任何想法?

下面是我使用 PropTypes 和 package.json 的代码。

警告:通过主 React 包访问 PropTypes 已被弃用, 并将在 React v16.0 中删除。使用最新的可用 v15。* 来自 npm 的 prop-types 包。

import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
// import PropTypes from 'prop-types';

export default function (AuthRequiredComponent) {
  class Authentication extends Component {
    static contextTypes = {
      router: PropTypes.object
    }

    componentDidMount() {
      if (!this.props.authenticated) {
        this.context.router.history.push('/');
      }
    }

    componentDidUpdate(nextProps) {
      if (!nextProps.authenticated) {
        this.context.router.history.push('/');
      }
    }

    render() {
      return <AuthRequiredComponent {...this.props} />;
    }
  }


  function mapStateToProps(state) {
    return {
      authenticated: state.auth.authenticated,
      email: state.auth.email
  };
 }
  return connect(mapStateToProps)(Authentication);
}

package.json:

"author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-loader": "^8.0.2",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "babel-preset-stage-0": "^6.24.1",
    "chai": "^3.5.0",
    "chai-jquery": "^2.0.0",
    "css-loader": "^0.28.11",
    "eslint-config-rallycoding": "^3.2.0",
    "mocha": "^2.4.5",
    "react-addons-test-utils": "^15.0.1",
    "style-loader": "^0.21.0",
    "webpack-dev-server": "^3.1.8"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "babel-core": "^6.2.1",
    "babel-polyfill": "^6.26.0",
    "babel-preset-stage-1": "^6.1.18",
    "cloudinary-core": "^2.5.0",
    "cloudinary-react": "^1.0.6",
    "dotenv-webpack": "^1.5.5",
    "file-loader": "^1.1.11",
    "jquery": "^2.2.4",
    "jsdom": "^8.1.0",
    "lodash": "^4.1.0",
    "prop-types": "^15.6.1",
    "react": "^15.6.2",
    "react-bootstrap": "^0.32.1",
    "react-day-picker": "^7.1.6",
    "react-dom": "^15.6.2",
    "react-dropzone": "^5.0.1",
    "react-helmet": "^5.2.0",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.0.0",
    "react-stripe-elements": "^2.0.1",
    "redux": "^4.0.0",
    "redux-form": "^6.8.0",
    "redux-thunk": "^2.2.0",
    "sha1": "^1.1.1",
    "webpack": "^4.17.2"
  }

【问题讨论】:

  • 为了后代,我终于摆脱了讨厌的 PropTypes 警告,方法是在我的文本编辑器 Atom 上搜索节点模块目录中的“{ PropTypes } from 'react'”并做出反应-router-dom 出现在结果中。显然,该模块(v4.0)仍然使用来自 react 的 PropTypes,升级到 v4.3.1 摆脱了警告。

标签: javascript reactjs react-proptypes


【解决方案1】:

你需要这样设置

import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';

export default function (AuthRequiredComponent) {
  class Authentication extends Component {

    componentDidMount() {
      if (!this.props.authenticated) {
        this.context.router.push('/');
      }
    }

    componentDidUpdate(nextProps) {
      if (!nextProps.authenticated) {
        this.context.router.push('/');
      }
    }

    render() {
      return <AuthRequiredComponent {...this.props} />;
    }
  }

  Authentication.propTypes = {
     router: PropTypes.object
  }

  function mapStateToProps(state) {
    return {
      authenticated: state.auth.authenticated,
      email: state.auth.email
  };
 }
  return connect(mapStateToProps)(Authentication);
}

【讨论】:

  • 您好,感谢您的回复。我进行了您建议的更改,现在我收到错误“身份验证未定义”。我尝试将代码放在其他地方,但我无法让它工作。
  • 抱歉,我们需要把它放在函数内部。检查我的更新答案
  • 现在我得到了未定义错误的历史记录。正如我所提到的,即使我没有在我的代码中使用 PropTypes 而是使用 props.history ,我也会收到警告,所以它必须来自其他依赖项,但我已经全部更新了它们......
  • 是的,也许。对于历史未定义你可以使用 this.context.router.push('/')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
相关资源
最近更新 更多