Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

reactjs - How to fix React 15.5.3 PropTypes deprecated warning when using create-react-app

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

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

I have already follow the blog:

npm install prop-types and import PropTypes from 'prop-types';

but it doesn't work. I don't use any PropTypes or props in code:

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

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

    ......
}

How to fix that?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...