Skip to content Skip to sidebar Skip to footer

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.

importReactfrom'react';

classAppextendsfromReact.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 herecomponentWillMount(nextProps, nextState){
        console.log('componentWillMount');
    }

    sample(){
        console.log('sample');
    }

    render(){
        return<divonClick={this.sample}>Hello World!</div>
    }
}

Solution 2:

This is what I would do to create a class in React:

importReact, { Component } from'react';

classGreeterFormextendsComponent {
    onFormSubmit = (e) => {
        e.preventDefault();
        //do stuff
    }
    render() {
        return (<ChildonFormSubmit={this.onFormSubmit} />)
    }
}

Post a Comment for "React - Components Using Javascript"