javascript - 反应:未捕获的类型错误:无法读取未定义的属性“删除”
问题描述
我的代码有问题:
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: Cannot read property 'remove' of undefined and Uncaught TypeError: Cannot read property 'remove' of undefined 请帮我解决这个问题!谢谢。
解决方案
推荐阅读
- java - 如何从同一个文本字段中获取两个值?
- python - Python机器学习对句子中的单词进行分类
- java - Java:如何使用函数式编程正确操作 BigDecimal 数组?
- mysql - 在忽略空值的情况下使用案例将 1 列拆分为多列
- reactjs - 无法解析“/Users/johnnynolan/Repos/simple-component-library/src”中的“./lib”
- mysql - 亚马逊红移禁用完整分组
- swift - ReactiveCocoa 内存泄漏 - UIButton 的 CocoaAction 防止 MVVM 对象反初始化
- c++ - 模板内嵌套类型名称的流式运算符重载
- java - 编程端ActionBar中可绘制的后退箭头ID
- java - 如何在 JRuby 中加载已编译的 Java 类?