未捕获类型错误:this.props.onDelete不是函数reactjs
Uncaught TypeError: this.props.onDelete is not a function reactjs
当我试图删除待办事项列表时,问题就会出现,onDelete不是一个函数。这里有完整的代码。感谢
var TodoList = React.createClass({
onDelete : function(key){
console.log('remove here');
},
render: function() {
var createItem = function(itemText,i) {
return (
<TodoListItem onDelete={this.remove} index={i} key=
{i}>{itemText}
</TodoListItem>
);
};
return(
<ul id="staggered-test" className="collection with-
header">
<li className="collection-header">
<h4>todo list</h4>
{this.props.items.map(createItem)}
</li>
</ul>
)
}
});
var TodoListItem = React.createClass({
remove : function(key){
console.log('test:',key);
console.log('that.props',this);
this.props.onDelete();
},
render : function(){
return(
<li className="collection-item">
<div>{this.props.children}
<a onClick=
{this.remove.bind(this,this.props.index)} href="#" className="secondary-content"><i className="material-icons">delete</i>
</a>
</div>
</li>
)
}
});
问题来自行
onDelete={this.remove}
未定义此删除函数。在onDelete中,您的wnat有什么要做的吗?如果没有,您可以删除this.props.onDelete();
问题来自这一行
onDelete={this.remove}
更改为
onDelete={this.onDelete}
所做的更改是{this.props.items.map(createItem)}
到{this.props.items.map(createItem,this)}
var TodoList = React.createClass({
remove : function(index){
console.log('remove from here:',this.props.items);
console.log('index:',index);
},
render: function() {
var createItem = function(itemText,i) {
return (
<TodoListItem onDelete={this.remove.bind(this,i)} index={i} key={i}>{itemText}
</TodoListItem>
);
};
return(
<ul id="staggered-test" className="collection
with-header">
<li className="collection-header">
<h4>todo list</h4>
{this.props.items.map(createItem,this)}
</li>
</ul>
)
}
});
类TodoListItem
中不需要函数remove
,因为父级具有该函数,因此它可以通过props
。
var TodoListItem = React.createClass({
render : function(){
return(
<li className="collection-item">
<div>{this.props.children}
<a onClick={this.props.onDelete} href="#"
className="secondary-content">
<i className="material-icons">delete</i>
</a>
</div>
</li>
)
}
});
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- jquery点击函数select&取消选择
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- Node.js v6.2.0类扩展不是函数错误
- 比较从函数和生成的日期对象
- jQuery中是否内置了任何字符串格式化函数
- 未捕获类型错误:this.props.onDelete不是函数reactjs