[Suggestion]: Improvements to an example in the document
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- JavaScript
- Sterne
- 11.8k
- Forks
- 7.9k
- Ø Merge
- 1 T. 11 Std.
- Gemergte PRs (30 T.)
- 11
Beschreibung
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 => )
}
}
)
}
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Beispiel auf der Seite zur Auswahl der Zustandsstruktur und vergleiche sein aktuelles Löschverhalten mit dem bereitgestellten Code von HookDemo und PlaceTree. Aktualisiere das Dokumentationsbeispiel so, dass das Löschen des einzigen untergeordneten Elements auch nun leere Vorfahren entfernt, und überprüfe anschließend den gerenderten Baum und das Löschverhalten im Beispiel.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, react, typescript
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Aktivitätsstatus
- Ruhig
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 72/100