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

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

課后作業(yè)刪除還是實現(xiàn)不出來

CommentList.js:

<button
type="submit"
className="btn?btn-danger?btn-sm?ml-3"
onClick={(e)?=>?{this.props.onDeleteComment(e)}}
>
刪除
</button>

我在CommentList.js中增加了一個刪除的button,點擊按鈕,可以調(diào)用App.js中的deleComment()方法,但是這個方法中一旦獲取this.state.comments就報錯,說是獲取不到comments。

App.js:

deleComment(){
//?console.log(111)
const?comments?=?this.state.comments
console.log(comments)
}


<CommentList
comments={comments}
onDeleteComment?=?{this.deleComment}
/>


正在回答

5 回答

獲得不了 this 就要想想 this 指向是否有問題,這個強調(diào)了多次 要注意。第二,就是你要刪除一條評論,是否要把要刪的是哪條評論傳出去呢?要不在你的 App 中怎么知道要刪除哪條?

0 回復 有任何疑惑可以回復我~

// 我來寫一下刪除按鈕的生成與功能的實現(xiàn),我是后端,沒學過ECMAScript

// 只是單單實現(xiàn)了這個功能,不知道在實現(xiàn)上對不對

// 當時老師寫這個組件的時候用的是函數(shù)式,我個人感覺這里使用函數(shù)式不方便就改成了類

// 還有就是不需要把當前組件的事件轉(zhuǎn)移到App.js中的吧

import React from 'react'

class CommentList extends React.Component{

? ? constructor(props){

? ? ? ? super(props)

? ? ? ? this.removeElement = this.removeElement.bind(this)

? ? }

? ? removeElement(event){

? ? ? ? event.target.parentElement.remove()

? ? }

? ? render() {

? ? ? ? const {comments} = this.props

? ? ? ? return (

? ? ? ? ? ? <div className="comment-list-component">

? ? ? ? ? ? ? ? <label htmlFor="">評論列表</label>

? ? ? ? ? ? ? ? <ul className="list-group mb-3">

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? comments.map((comment, index) =>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <li key={index} className="list-group-item">{comment}&nbsp;&nbsp;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <a

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? className="btn btn-link text-danger"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? onClick={this.removeElement}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? >刪除</a>

? ? ? ? ? ? ? ? ? ? ? ? ? ? </li>

? ? ? ? ? ? ? ? ? ? ? ? )

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? </ul>

? ? ? ? ? ? </div>

? ? ? ? )

? ? }

}

export default CommentList



0 回復 有任何疑惑可以回復我~

講師都告訴你了沒傳索引

0 回復 有任何疑惑可以回復我~

他這個應該怎么寫我也不知道? this指向沒有問題吧?不解

0 回復 有任何疑惑可以回復我~

this指向的問題吧

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

課后作業(yè)刪除還是實現(xiàn)不出來

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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