Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable React.createClass and PropTypes deprecated warnings in babel react present

At latest React 15.5.1 package, If using babel react present to resolve jsx file, will appears following warnings:

Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.  warning.js:36 Warning: A Component: React.createClass is deprecated and will be removed in version 16. Use plain JavaScript classes instead. If you're not yet ready to migrate, create-react-class is available on npm as a drop-in replacement. 

My code is following:

import React from 'react' import ReactDOM from 'react-dom';  class Counter extends React.Component {   constructor(props) {     super(props);     this.state = {       count: 1     }   }   componentDidMount() {     setInterval( ()=> {       this.setState((prevState, props) => ({         count: prevState.count + 1       }))     }, 1000)   }   render(){     return (       <h1>{this.state.count}</h1>     )   } }  const root = document.createElement('div'); root.id = 'app'; document.body.appendChild(root);  ReactDOM.render(   <Counter />,   document.querySelector('#app') ); 

I am not using React.createClass and PropTypes in my code, It seems babel transform my code to React.createClass and PropTypes, How to fix that?

like image 583
TangMonk Avatar asked Apr 08 '17 01:04

TangMonk


People also ask

Is proptypes deprecated in react 16?

Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead. warning.js:36 Warning: A Component: React.createClass is deprecated and will be removed in version 16. Use plain JavaScript classes instead.

What to do if createclass is deprecated in react?

Use the prop-types package from npm instead. warning.js:36 Warning: A Component: React.createClass is deprecated and will be removed in version 16. Use plain JavaScript classes instead. If you're not yet ready to migrate, create-react-class is available on npm as a drop-in replacement.

What happened to proptype validation in react?

In a future major release of React, the code that implements PropType validation functions will be stripped in production. Once this happens, any code that calls these functions manually (that isn’t stripped in production) will throw an error. The normal usage of PropTypes is still supported:

How to run typecheck on react props?

For some applications, you can use JavaScript extensions like Flow or TypeScript to typecheck your whole application. But even if you don’t use those, React has some built-in typechecking abilities. To run typechecking on the props for a component, you can assign the special propTypes property:


1 Answers

React 15.5.0 contain new warnings belong to the changes that are coming on React 16, the warning that you are getting, is actually telling you that you have to implement the new way (since the current way that you are using is going to be deprecated on 16).

React.createClass, you have 2 options, first one is to just use plain JS syntax:

function Welcome(props) {   return <h1>Hello, {props.name}</h1>; } 

or use the create-react-class module (availiable on npm):

// Before (15.4 and below) var React = require('react');  var Component = React.createClass({   mixins: [MixinA],   render() {     return <Child />;   } });  // After (15.5) var React = require('react'); var createReactClass = require('create-react-class');  var Component = createReactClass({   mixins: [MixinA],   render() {     return <Child />;   } }); 

About the PropTypes warning, check if you are using module that using PropTypes, sometimes it's coming from external part.

Anyway, to read more about it, you are welcome to get Facebook blog post about those changes

like image 189
Idan Gozlan Avatar answered Sep 29 '22 06:09

Idan Gozlan