添加删除按钮错误TypeError: Cannot read property 'props' of undefined

来源:4-2 留言本实例编码

Yvonne_D

2018-11-19 07:12

在commentList.js中:

<ul className="list-group mb-3">               {comments.map((comment,index)=>               	<li key={index} className="list-group-item">{comment}		<button  onClick={ (e)=> this.props.onDeleteComment(e)}>X</button>               	</li>                      )}            </ul>

APP.js中:

<CommentList comments={this.state.comments} onDeleteComment={this.state.comments.deleteComment}/>
 deleteComment(index) {    let newComments = this.state.comments    newComments.splice(index, 1)    this.setState({      comments: newComments    })  }

这个删除按钮如何做呢?每次点击删除就会显示props undefined

写回答 关注

1回答

  • pardon110
    2018-11-19 13:28:48

    注意this指向及删除按钮中回调的index参数传递

基于实例的 React16 傻瓜课程

通过一系列简单有趣的例子,让你了解 React16 的基本概念和新特性

22325 学习 · 103 问题

查看课程

相似问题