添加刪除按鈕錯誤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
2018-11-19
注意this指向及刪除按鈕中回調(diào)的index參數(shù)傳遞