課后作業(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} />
2018-10-30
獲得不了 this 就要想想 this 指向是否有問題,這個強調(diào)了多次 要注意。第二,就是你要刪除一條評論,是否要把要刪的是哪條評論傳出去呢?要不在你的 App 中怎么知道要刪除哪條?
2018-12-04
// 我來寫一下刪除按鈕的生成與功能的實現(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}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? <a
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? className="btn btn-link text-danger"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? onClick={this.removeElement}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? >刪除</a>
? ? ? ? ? ? ? ? ? ? ? ? ? ? </li>
? ? ? ? ? ? ? ? ? ? ? ? )
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? </ul>
? ? ? ? ? ? </div>
? ? ? ? )
? ? }
}
export default CommentList
2018-11-19
講師都告訴你了沒傳索引
2018-11-11
他這個應該怎么寫我也不知道? this指向沒有問題吧?不解
2018-10-28
this指向的問題吧