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 摘要。