搜索

首页  >  问答  >  正文

javascript - react使用了this.state.就会报错

滿天的星座滿天的星座2864 天前508

全部回复(4)我来回复

  • 我想大声告诉你

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

    class 是ES6的写法。
    getIntialState是ES5的hook函数,不能在这里用。

    回复
    0
  • 大家讲道理

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

    定义初始state应该在constructor中:
    constructor(props){

    1

    2

    3

    4

    <code>super(props);

    this.state={

          ss:"xxx"

    }</code>

    }

    回复
    0
  • 迷茫

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

    感觉this.handlechange这里写的有问题,constructor里需要绑定一下这个方法,否则this丢了应该找不到这个方法?

    1

    2

    3

    4

    <code>constructor(props) {

        super(props)

        this.handleChange = this.handleChange.bind(this)

    }</code>

    回复
    0
  • 阿神

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

    如果不想用constructor,直接定义state 在class从 直接写
    `
    class ... {

    state = {

    1

    <code>sss</code>

    }

    // this.state 可以获取
    }
    `

    回复
    0
  • 取消回复