Javascript 我对setState有一些问题,并有一条消息:无法读取属性';设置状态';空的

Javascript 我对setState有一些问题,并有一条消息:无法读取属性';设置状态';空的,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,请帮忙。在我的代码中出现了一些错误,但我无法理解到底是什么,可能是语法错误,但我在这里找不到: import React, { Component } from 'react'; class Note extends Component { constructor(props) { super(props); this.state = { editing: false } } edit() { **this.setState({ edit

请帮忙。在我的代码中出现了一些错误,但我无法理解到底是什么,可能是语法错误,但我在这里找不到:

import React, { Component } from 'react';

class Note extends Component {
  constructor(props) {
    super(props);
    this.state = {
      editing: false
    }
  }
  edit() {
    **this.setState({ editing: true })** // probaly problem here
  }
  save() {
    this.setState({ editing: false }) // here
  }
  remove() {
    alert("Removing Note") //and here
  }
  renderForm() {
    return (
      <div className="note">
        <textarea></textarea>
        <button onClick={ this.save }></button>
      </div>
    )
  }
  renderDisplay() {
    return (
      <div className="note">
        <p>{ this.props.children }</p>
        <span>
          <button onClick={ this.edit }>EDIT</button>
          <button onClick={ this.remove }>X</button>
        </span>
      </div>
    )
  }
  render() {
    if( this.state.editing ) {
      return this.renderForm()
    } else {
      return this.renderDisplay()
    }
  }
}

export default Note;
import React,{Component}来自'React';
类注释扩展了组件{
建造师(道具){
超级(道具);
此.state={
编辑:假
}
}
编辑(){
**this.setState({editing:true})**//此处可能存在问题
}
保存(){
this.setState({editing:false})//此处
}
删除(){
警报(“删除注释”)//此处
}
renderForm(){
返回(
)
}
renderDisplay(){
返回(
{this.props.children}

编辑 X ) } render(){ 如果(this.state.editing){ 返回此 }否则{ 返回这个。renderDisplay() } } } 导出默认票据;
来自chrome的消息是:“无法读取null的属性'setState'”
在第bold
this.setState({editing:true})行上
,单击“编辑”按钮后,

您的方法需要绑定到类

constructor(props) {
  super(props);
  this.state = {
    editing: false
  }
  this.edit = this.edit.bind(this);
  this.save = this.save.bind(this);
}

还有一个实验性的/建议性的特性,通过使用arrow函数作为类方法,您可以在不显式绑定的情况下绑定这些函数。有更多信息。

非常感谢,它正在运行。