Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class extends React.Component can't use getInitialState in React

Tags:

reactjs

I'm tring the ES6 syntax in React, and write the components like:

export default class Loginform extends React.Component {
    getInitialState() {
        return {
          name: '',
          password: ''
        };
    };
}

but the browser throws me a waring about:

Warning: getInitialState was defined on Loginform, a plain JavaScript class. This is only supported for classes created using React.createClass. Did you mean to define a state property instead?

I can handle it with the traditional syntax var Loginform = React.createClass but what's correct ES6 syntax?

Another little thing, I think in traditional syntax React.createClass is an object, so the functions in it is separated by comma, but with the extends class it require semicolon, I don't understand it well.

like image 362
Brick Yang Avatar asked Oct 31 '15 01:10

Brick Yang


People also ask

What is getInitialState in React?

getInitialState is the ES5 friendly method to define the initial state of a React component. JavaScript React. One fairly popular question that got asked on programming bulletin boards has to do with the similarities and differences between React's constructor and the built in method getInitialState .

Why can't we declare a class with the class keyword in React?

Explanation: The only reason behind the fact that it uses className over class is that the class is a reserved keyword in JavaScript and since we use JSX in React which itself is the extension of JavaScript, so we have to use className instead of class attribute.


3 Answers

You don't need semicolons or commas between ES6 class method declarations.

For ES6 classes, getInitialState has been deprecated in favor of declaring an initial state object in the constructor:

export default class Loginform extends React.Component {
  constructor(props, context) {
    super(props, context);

    this.state = {
      name: '',
      password: ''
    };
  };
}
like image 159
max Avatar answered Oct 22 '22 16:10

max


ES6 example: state, defaultProps, propTypes

import React from 'react'
import ReactDom from 'react-dom';
export default class Item extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            check:false,
        };
        this.click=this.click.bind(this);
    }
    click(){
       this.setState({check:true});
    }
    render(){
        const text=this.state.check?'yes':'no';
        return(<div onClick={this.click}>{this.props.name} : <b>{text}</b></div>)
    }

}

Item.defaultProps={
    comment:"default comment",
};
Item.propTypes={
    name:React.PropTypes.string.isRequired,
};
like image 24
Juan Carlos Constantine Avatar answered Oct 22 '22 17:10

Juan Carlos Constantine


If we use class field, following is working.

state = {
      name: '',
      password: ''
}

This can be used instead of

constructor(props, context) {
    super(props, context);

    this.state = {
      name: '',
      password: ''
    };
  };
like image 44
lingjin.w Avatar answered Oct 22 '22 15:10

lingjin.w