[Suggestion]: remove the caveat for writing ref in render path
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
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
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
Überprüfe den Abschnitt zu den useRef-Einschränkungen unter https://react.dev/reference/react/useRef#caveats und vergleiche seine Hinweise mit dem im Bericht verlinkten Apollo Client-Issue und der useLatest-Implementierung. Kläre, ob die Einschränkung für Refs entfernt oder überarbeitet werden sollte, die während des Renderns synchronisiert werden, ohne den Rückgabewert der Komponente zu beeinflussen, aktualisiere anschließend die Dokumentation und verifiziere, dass die resultierende Formulierung eindeutig ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, react
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100