反应:未捕获的TypeError:无法读取未定义的属性“删除”

时间:2020-02-28 11:30:17

标签: javascript reactjs

我的代码有问题:

class Note extends React.Component{
  constructor(props){
    super(props);
  }
  xoa(){
    var {index, handleRemove} = this.props;
    handleRemove(index);
  }
  render(){
    return(
      <div>
        <p>{this.props.children}</p>
        <button onClick={this.xoa.bind(this)}> Delete </button>
      </div>
    );
  }
}

class List extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      arrayNote: ['ha', 'hi', 'hu']
    };
  }
  remove(index){
    this.state.arrayNote.splice(index, 1);
    this.setState(this.state);
  }
  render(){
    this.remove = this.remove.bind(this);
    return(
      <div>{this.state.arrayNote.map(function(note, i){
          return (<Note index={i}
            handleRemove={this.remove}
            key={i}>{note}</Note>)
        })}
      </div>
    );
  }
}

ReactDOM.render(
  <List />,
  document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.11.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script>

在控制台中,它显示:TypeError:无法读取未定义和未捕获的属性'remove'TypeError:无法读取未定义的属性'remove' 请帮我解决这个问题! 谢谢。

0 个答案:

没有答案