search

Home  >  Q&A  >  body text

javascript - react will report an error when using this.state.

滿天的星座滿天的星座2863 days ago504

reply all(4)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:31:18

    class is the ES6 way of writing.
    getIntialState is an ES5 hook function and cannot be used here.

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-19 10:31:18

    Define the initial state should be in the constructor:
    constructor(props){

    super(props);
    this.state={
          ss:"xxx"
    }

    }

    reply
    0
  • 迷茫

    迷茫2017-05-19 10:31:18

    I feel like there is something wrong with what is written here. This method needs to be bound in the constructor, otherwise this method should not be found if this is lost? this.handlechange

    constructor(props) {
        super(props)
        this.handleChange = this.handleChange.bind(this)
    }

    reply
    0
  • 阿神

    阿神2017-05-19 10:31:18

    If you don’t want to use constructor, define state directly and write directly in class from
    `
    class... {

    state = {

    sss

    }

    // this.state can be obtained
    }
    `

    reply
    0
  • Cancelreply