[Suggestion]: Improvements to an example in the document
まだ誰も着手していません。
- 主要言語
- JavaScript
- スター
- 11.8k
- フォーク
- 7.9k
- 平均マージ
- 1日 11時間
- マージ済み PR(30日)
- 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 => )
}
}
)
}
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
state 構造の選択に関するページの例から始め、現在の削除動作を、提供されている HookDemo と PlaceTree のコードと比較します。ドキュメントの例を更新し、唯一の子を削除すると空になった祖先も削除されるようにしてから、例におけるレンダリングされたツリーと削除動作を確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, react, typescript
- 領域
- documentation
- issue の種類
- ドキュメント
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 静か
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 72/100