[英]React - Components using Javascript
I am trying to figure out how to respond to the warning in react to use javascript classes to create components in my MERN app. 我试图弄清楚如何响应警告以使用javascript类在我的MERN应用中创建组件。
The warning says: 警告说:
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]
The link in that message says: 该消息中的链接显示:
// After (15.5)
var React = require('react');
var createReactClass = require('create-react-class');
var Component = createReactClass({
mixins: [MixinA],
render() {
return <Child />;
}
});
I am using react v 15.5.4 我正在使用React v 15.5.4
In my app, I have tried to change my components as follows: 在我的应用中,我尝试如下更改组件:
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();
However, the warning persists. 但是,警告仍然存在。 Can anyone see what I have done wrong?
谁能看到我做错了吗? How do I implement the new approach to defining components?
如何实现定义组件的新方法?
You should use ES6 class for make a React component. 您应该使用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>
}
}
This is what I would do to create a class in React: 这就是我要在React中创建一个类的方法:
import React, { Component } from 'react';
class GreeterForm extends Component {
onFormSubmit = (e) => {
e.preventDefault();
//do stuff
}
render() {
return (<Child onFormSubmit={this.onFormSubmit} />)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.