我正在尝试弄清楚如何响应 React 中的警告,以使用 javascript 类在我的 MERN 应用程序中创建组件。
警告说:
Warning: Accessing createClass via the main React package is deprecated, and will be removed in React v16.0. Use a plain JavaScript class instead. If you're not yet ready to migrate, create-react-class v15.* is available on npm as a temporary, drop-in replacement. For more info see[ \[this link\][1]
该消息中的链接显示:
// After (15.5)
var React = require('react');
var createReactClass = require('create-react-class');
var Component = createReactClass({
mixins: [MixinA],
render() {
return <Child />;
}
});
我使用的是 React v 15.5.4
在我的应用程序中,我尝试按如下方式更改我的组件:
import React from 'react';
import ReactDOM from 'react-dom';
import { Button } from 'react-bootstrap';
var createReactClass = require('create-react-class');
var GreeterForm = createReactClass({
onFormSubmit: function(e) {
e.preventDefault();
但是,警告仍然存在。谁能看到我做错了什么吗?如何实现定义组件的新方法?
最佳答案
您应该使用 ES6 类来制作 React 组件。
import React from 'react';
class App extends from React.Component{
constructor(props){
super(props);
this.sample = this.sample.bind(this);
// initialize your methods, states here
}
// if you want life cycle methods and methods define here
componentWillMount(nextProps, nextState){
console.log('componentWillMount');
}
sample(){
console.log('sample');
}
render(){
return <div onClick={this.sample}>Hello World!</div>
}
}
关于javascript - React - 使用 Javascript 的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770640/