如何使用create-react-app修复React 15.5.3 PropTypes弃用警告

Con*_*tra 18 reactjs create-react-app

我正在使用create-react-app来启动React项目.在最新的React 15.5.3软件包中,它出现以下警告:

警告:不建议通过主React包访​​问PropTypes.请改用npm中的prop-types包.

我已经关注了博客:

npm install prop-typesimport PropTypes from 'prop-types';

但它不起作用.我不使用任何代码PropTypesprops代码:

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

class App extends Component {
    constructor() {
        super();
        this.state = {
            videoVisible: true,
        };
    }

    ......
}
Run Code Online (Sandbox Code Playgroud)

如何解决?

谢谢.

Spe*_*gum 43

从Reacts博客 - npm安装prop-types,然后使用新代码.此外,它说如果嵌套组件不使用prop-types但父级是 - 所以你需要检查其他组件,你可以收到此错误消息.

// Before (15.4 and below)
import React from 'react';

class Component extends React.Component {
  render() {
    return <div>{this.props.text}</div>;
  }
}

Component.propTypes = {
  text: React.PropTypes.string.isRequired,
}

// After (15.5)
import React from 'react';
import PropTypes from 'prop-types';

class Component extends React.Component {
  render() {
    return <div>{this.props.text}</div>;
  }
}

Component.propTypes = {
  text: PropTypes.string.isRequired,
};
Run Code Online (Sandbox Code Playgroud)

  • 你能把代码放在github上吗?您也可以不使用道具,但其他库仍然可以使用,例如Redux. (2认同)
  • 哦,是的,正如你所说,我使用aframe-react,它使用道具.我已经修好了.谢谢! (2认同)