怎么把commentLists中的index傳遞到APP.js中
commentsList.js?handleDelete(index){console.log(index)this.props.delete(index);}render?(){const?{comments}?=?this.props;return?(<div?className="comment-list-compenent"><label>評論列表</label><ul?className="list-group?mb-3">{comments.map((comment,?index)?=><likey={index}className="list-group-item"onClick?=?{this.handleDelete}index?=?{index}>{comment}</li>)}</ul></div>)}
DeleteItem(index){
const list = [...this.state.comments];
list.splice(index, 1);
this.setState({
comments: list //如果key和值是一樣的直接寫一個就行了
});
}
2019-10-11
這里有一個很關(guān)鍵的地方需要注意。子組件純函數(shù)是沒有this 的,所以通過
是無法調(diào)用的。正確的方法應(yīng)該是在子組件純函數(shù)的頭部引入父組件的:函數(shù)名、參數(shù)變量,如下:
這個時候,才能在子組件的純函數(shù)內(nèi)使用:
2019-03-06
你這沒有把index傳出去啊,可以用箭頭函數(shù)
App.js中