Tan*_*onk 34 javascript babel reactjs
最新的React 15.5.1包,如果使用babel react present来解析jsx文件,将出现以下警告:
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.
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
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')
);
Run Code Online (Sandbox Code Playgroud)
我没有在我的代码中使用React.createClass和PropTypes,似乎babel将我的代码转换为,React.createClass以及PropTypes如何解决这个问题?
Ida*_*lan 11
React 15.5.0包含属于React 16上发生的更改的新警告,您收到的警告实际上是告诉您必须实现新方法(因为您当前使用的方式将是不推荐使用16).
React.createClass,你有2个选项,第一个是使用普通的JS语法:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
Run Code Online (Sandbox Code Playgroud)
或使用该create-react-class模块(可在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 />;
}
});
Run Code Online (Sandbox Code Playgroud)
关于PropTypes警告,检查您是否使用使用PropTypes的模块,有时它来自外部部件.
无论如何,要了解更多相关信息,欢迎您访问有关这些更改的Facebook博文
React v15.5.0实现了新的警告
将React降级为15.4.x对我有用
npm install --save react@15.4.0 react-dom@15.4.0
Run Code Online (Sandbox Code Playgroud)
N. *_*mon -4
你不应该扩展react.Component。您需要从 React 导入组件并扩展它,如下所示:
import React, {Component} from 'react';
class Counter extends Component{
//your code here
render (){
return (
<h1>{this.state.count}</h1>
);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处了解差异和 ES6 类
| 归档时间: |
|
| 查看次数: |
22144 次 |
| 最近记录: |