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. The warning says: Warning: Accessing createClass via
Solution 1:
You should use ES6 class for make a React component.
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>
}
}
Solution 2:
This is what I would do to create a class in React:
import React, { Component } from 'react';
class GreeterForm extends Component {
onFormSubmit = (e) => {
e.preventDefault();
//do stuff
}
render() {
return (<Child onFormSubmit={this.onFormSubmit} />)
}
}
Post a Comment for "React - Components Using Javascript"