useInsertionEffect other use-case
Nessuno ha ancora preso questa issue.
- Lingua principale
- JavaScript
- Stelle
- 11.8k
- Fork
- 7.9k
- Merge medio
- 1g 11h
- PR unite (30g)
- 11
Descrizione
Hello,
in the docs it is stated that useInsertionEffect should be used only by css-in-js library authors, I think I've came across an other use case. I'm using forwardRef with a type HTMLDivElement[] where through a callback ref I'm pushing the refs in to the array as so ref={(ref) => ref && innerRef.current.push(ref)} and syncing it outside with a useImperativeHandle
However this needs a cleanup whenever the component re-renders, since useInsertionEffect fires sync BEFORE the dom is mutated, it is perfect for the cleanup of the innerRef array.
The code is as follows
const ComponentReturningAnArray = React.forwardRef<HTMLDivElement[], { data: string[] }>(({array}, outerRef) => {
const innerRef = useRef<HTMLDivElement[]>([]);
useImperativeHandle<HTMLDivElement[], HTMLDivElement[]>(outerRef, () => innerRef.current, [innerRef.current]);
useInsertionEffect(() => {
innerRef.current = [];
});
return (
<>
{
data.map((d) => (<div ref={(ref) => ref && innerRef.current.push(ref)}>{d}</div>))
}
</>
)
});
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'affermazione della documentazione relativa a useInsertionEffect e con l'esempio fornito ComponentReturningAnArray. Verifica se questa pulizia della raccolta di ref è un caso d'uso previsto e determina come la documentazione dovrebbe descriverla. Il lavoro è completato quando le indicazioni riflettono accuratamente i casi d'uso supportati e l'esempio viene affrontato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, react
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100