reactjs / reactjs/react.dev

[Suggestion]: Improvements to an example in the document

未關閉 適合新手
#8,141 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

type: documentation
主要語言
JavaScript
星號
11.8k
分支
7.9k
平均合併
1 天 11 小時
30 天內合併 PR
11

描述

Summary

I learned a lot from this example. After understanding it more deeply, I discovered that if a child item is the only child of its parent item, the parent item should also be deleted after deleting it. The original documentation didn't consider this, and there were many details to pay attention to during implementation. I finally completed it, and I'll post my code below.

Page

https://react.dev/learn/choosing-the-state-structure

Details

import {useImmer} from 'use-immer'
import {initialTravelPlan} from './travel.ts'

interface PlaceTreeProps {
id: number
parentId: number
plan: TravelPlan
onComplete: (parentId: number, id: number) => void
}

interface TravelPlan {
[key: number]: {
id: number
title: string
childIds: number[]
}
}

export default function HookDemo() {
const [plan, setPlan] = useImmer(initialTravelPlan)
const root = plan[0]
const planetIds = root.childIds
const handleComplete = (parentId: number, id: number) => {
setPlan(draft => {
if (id===0) return
handleChildrenDelete(id)
handleParentDelete(parentId,id)

        function handleParentDelete(pId: number,cId: number) {
            const parent = draft[pId]
            if (!parent) return
            parent.childIds = parent.childIds.filter((childId) => childId !== cId)
            **// if the parent has no more children, delete it too**
            if (parent.childIds.length === 0 && pId !== 0) {
                const grandParentId = Object.values(draft).find(p => p.childIds.includes(pId))?.id
                if (grandParentId !== undefined) {
                    handleParentDelete(grandParentId, pId)
                }
                delete draft[pId]
            }
        }

        function handleChildrenDelete(childId: number) {
            const child = draft[childId]
            if (!child) return
            child.childIds.forEach((grandChildId) => {
                handleChildrenDelete(grandChildId)
            })
            delete draft[childId]
        }
    })
    console.log(parentId, id)
}
return (
    <div>
        <h2>Place to Visit</h2>
        <ol>
            {planetIds.map((id) => (
                <PlaceTree
                    id={id}
                    key={id}
                    parentId={0}
                    plan={plan}
                    onComplete={handleComplete}
                />
            ))}
        </ol>
    </div>
)

}

function PlaceTree({id, parentId, plan, onComplete}: PlaceTreeProps) {
const place = plan[id]
const childIds = place.childIds
return (


  • {place.title}
    <button type={'button'} onClick={() => onComplete(parentId, id)}>
    Del

    {
    childIds.length > 0 &&

      {
      childIds.map(childId => )
      }

    }

  • )
    }

    貢獻指南

    開啟貢獻指南

    從這裡開始

    1. 先讀完整個 Issue,再讀專案的貢獻指南。
    2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
    3. Fork 儲存庫,在一個分支上完成修改。
    4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

    研究方向

    從選擇狀態結構頁面上的範例開始,將其目前的刪除行為與提供的 HookDemo 和 PlaceTree 程式碼進行比較。更新文件範例,使刪除唯一的子項也會移除現在為空的祖先,然後驗證範例中呈現的樹狀結構和刪除行為。

    由索引模型根據 Issue 內容生成。

    評估

    技術堆疊
    javascript, react, typescript
    領域
    documentation
    Issue 類型
    文件
    難度
    2/5
    預估耗時
    1-3 小時
    活躍度
    冷清
    描述清晰度
    描述清楚
    新手友好度
    72/100

    把新 issue 寄到你的電子郵件信箱

    精選適合新手參與的 GitHub issue 摘要。