[Suggestion]: Improvements to an example in the document
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 7.9k
- Merge medio
- 1g 11h
- PR unite (30g)
- 11
Descrizione
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 => )
}
}
)
}
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l’esempio nella pagina sulla scelta della struttura dello stato e confronta il suo comportamento attuale durante l’eliminazione con il codice fornito di HookDemo e PlaceTree. Aggiorna l’esempio della documentazione in modo che l’eliminazione dell’unico elemento figlio rimuova anche gli antenati ora vuoti, quindi verifica l’albero renderizzato e il comportamento di eliminazione nell’esempio.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, react, typescript
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Tranquilla
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 72/100