第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定

怎么把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和值是一樣的直接寫一個就行了

});

}


正在回答

2 回答

這里有一個很關(guān)鍵的地方需要注意。子組件純函數(shù)是沒有this 的,所以通過

this.props.XXX

是無法調(diào)用的。正確的方法應(yīng)該是在子組件純函數(shù)的頭部引入父組件的:函數(shù)名、參數(shù)變量,如下:

const?CommentList?=?({comments,onDeleteThis})?=>?{}

這個時候,才能在子組件的純函數(shù)內(nèi)使用:

comments,onDeleteThis


0 回復(fù) 有任何疑惑可以回復(fù)我~

你這沒有把index傳出去啊,可以用箭頭函數(shù)

onClick={()=>{this.props.deleteComment(index)}}

App.js中

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


0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消

怎么把commentLists中的index傳遞到APP.js中

我要回答 關(guān)注問題
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號