[Suggestion]: remove the caveat for writing ref in render path
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
In the doc for ref https://react.dev/reference/react/useRef#caveats, it calls out to not read/write ref in rendering phase without a very clear "bad example"
Page
No response
Details
However, there is use case where the caller just needs a static ref at component level to wrap a dynamic value: for example, I need to read/write a static ref in rendering path (keep it in-sync with the current render context value)
In another word, the argument could be "a need to read/sync a "render context" value into a static variable attached to the functional component", + the "ref" value is not use in the "return" of the functional component
const { componentId, renderId } = useContext(componentContext);
const context = { componentId, renderId } <--- dynamic
const contextRef = useRef(context); <--- static
contextRef.current = context;
useQuery({
context: contextRef <--- static ref to avoid equal check, the ref value is only used for logging
})
e.g. https://github.com/apollographql/apollo-client/issues/11835
and there are popular library like useLatest implemented by writing ref in render path
Can we remove or update the caveat to make it more clear? E.g. adding
If you don't attempt to use the "mutable" ref in determining the function return, it is fine to use it to sync rendering context
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
Esamina la sezione sulle avvertenze di useRef all’indirizzo https://react.dev/reference/react/useRef#caveats e confronta le indicazioni con l’issue di Apollo Client e l’implementazione di useLatest collegati nel report. Chiarisci se l’avvertenza debba essere rimossa o rivista per i refs sincronizzati durante il rendering senza influire sul valore restituito dal componente, quindi aggiorna la documentazione e verifica che la formulazione risultante sia inequivocabile.
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