我正在開發一個評論系統,并使用遞回方法來顯示父子評論。當我洗掉一個孩子時,我想更新其父級的子評論數。
//json data data = [{ name: 'Parent1', childComments: [{ name: 'Child1', text:'child comment1' }, { name: 'Child2', text:'child comment2' }] },{ name: 'Parent2', childComments: [{ name: 'Child1', text:'child comment1' }, { name: 'Child2', text:'child comment2' }] }]
const Comment = ({ item }) => {
const childComment = item
//delete child element and update the parent count
const deleteChildComment =(item) => {
}
return childComment && (
<>
{name}
Children count : {childComments.length}
<Button
className={styles.replies}
onClick={() => {
setNewCommentAdded(false)
deleteChildComment(childComment)
}}
> Delete This comment </Button>
{childComments && items.map((item) => (
<Comment item={item} />
))}
</>
)
}
uj5u.com熱心網友回復:
數孩子數是個壞主意,會使事情變得太復雜。只需使用您用于生成評論組件的資料即可。
通常,始終讓 UI 表示資料。不要嘗試從您的 UI 布局中獲取資料。
例如
commentList.map((comment) => (<Comment childCount={comment.children.length}));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/398862.html
標籤:javascript 反应 反应原生 jsx
