问答详情
源自:4-2 留言本实例编码

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

在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

提问者:Yvonne_D 2018-11-19 07:12

个回答

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

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