使用create-react-app时如何修复React 15.5.3 PropTypes不建议使用的警告 [英] How to fix React 15.5.3 PropTypes deprecated warning when using create-react-app

查看:173
本文介绍了使用create-react-app时如何修复React 15.5.3 PropTypes不建议使用的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 create-react-app 开始反应项目。
最新的 React 15.5.3 软件包中,显示以下警告:

I'm using create-react-app to start React project. At latest React 15.5.3 package, it appears following warnings:


警告:不赞成通过主React包访​​问PropType。
请使用npm中的prop-types包。

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

我已经遵循博客

npm install prop-type 从'prop-types'导入PropType;

,但不起作用。
我在代码中不使用任何 PropTypes props

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

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

    ......
}

如何解决此问题?

谢谢。

推荐答案

从Reacts博客中提取-npm安装prop-types,然后使用新代码。它还说如果嵌套组件不使用prop-type但父组件是-则可以得到此错误消息。因此,您需要检查其他组件。

Pulled from Reacts blog - npm install prop-types, then use new code. Also it said you can get this error message if a nested component is not using prop-types but the parent is - so you need to check other components.

// 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,
};

这篇关于使用create-react-app时如何修复React 15.5.3 PropTypes不建议使用的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆