[Suggestion]: lazy initialization example can encourage unsafe patterns - show example
Los mantenedores suelen responder en 3 días
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 82/100
- Tipo de issue
- Documentación
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- javascript, react
- Área
- documentation
Línea de trabajo
Comienza con la sección «lazy initialization» de la página enlazada de documentación de React. Aclara que crear recursos gestionados por el ciclo de vida durante el renderizado puede ser inseguro cuando un renderizado no se confirma, y muestra la diferencia con la lazy initialization segura; se considera terminado cuando los lectores puedan distinguir qué patrones necesitan un Effect.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Summary
Show example when lazy initialization example can be unsafe.
Page
https://react.dev/reference/rules/components-and-hooks-must-be-pure#lazy-initialization
Details
The lazy initialization section currently presents this pattern as valid:
function ExpenseForm() {
SuperCalculator.initializeIfNotReady(); // ✅ Good: if it doesn't affect other components
// Continue rendering...
}
However, I think this example is easy to generalize into a dangerous pattern:
if (ref.current === null) {
ref.current = createResource();
}
For resources with a lifecycle - subscriptions, event listeners, timers, connections, etc. - creating the resource during render is unsafe because a render is not guaranteed to result in a committed effect.
For example:
function useLegacyStore() {
const subscriptionRef = useRef(null);
if (subscriptionRef.current === null) {
subscriptionRef.current = store.subscribe(() => {
// ...
});
}
useEffect(() => {
return () => {
subscriptionRef.current?.unsubscribe();
};
}, []);
}
This can appear to work when components only render as part of normal mount/unmount flows. However, with concurrent rendering or APIs such as Activity, React may render a component without subsequently mounting the
Effect associated with that render.
The subscription has already been created, while its cleanup is tied to an Effect that may never run.
The documentation should make this distinction explicit, perhaps by adding a warning/example.
Inspired by:
https://hackernoon.com/react-activity-when-a-render-no-longer-guarantees-an-effect
- Lenguaje dominante
- JavaScript
- Estrellas
- 11.8k
- Forks
- 8k
- Merge medio
- 3 d 5 h
- PR fusionados (30 d)
- 9
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de reactjs/react.dev
-
[Suggestion]: Document that useFormStatus works with a preventDefault-ed onSubmit + startTransitionPosiblemente ocupada @Felix-ming la tomó hace 2 días. Abiertotype: documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
reactjs/react.dev#8682 · 1 comentario ·
Los mantenedores suelen responder en 3 días
-
type: documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
reactjs/react.dev#8661 · 1 comentario · 7 reacciones ·
Los mantenedores suelen responder en 3 días
-
docs: improve useState explanation in quick startPosiblemente ocupada @tirthraj01 la tomó hace 26 días. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
Los mantenedores suelen responder en 3 días
-
[Bug]: Next button throws a runtime error instead of cycling to the first sculpturePosiblemente ocupada @Saurav10codes la tomó hace 34 días. Abiertobug: unconfirmed
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
reactjs/react.dev#8634 · 3 comentarios ·
Los mantenedores suelen responder en 3 días
-
[Typo]: Repeaded words in Pitfall sectionPosiblemente ocupada @mdimamhosen la tomó hace 36 días. Abiertotype: typos
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
reactjs/react.dev#8622 · 1 comentario ·
Los mantenedores suelen responder en 3 días
Todos los issues de reactjs/react.dev
Issues similares
-
ci-install-db-tools stall-case tests flake: stalled apt-get can be killed before it logs its callAbiertoeffort:low model:light plan planner:opus-5-5 tests
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
Los mantenedores suelen responder en 1 día
-
Bug 🐞
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
mozilla-mobile/firefox-ios#35986 ·
Los mantenedores suelen responder en 1 día
-
Hidden loading spinner keeps animating after connect, causing high idle CPU usage in FirefoxAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
-
bug(sight): the dashboard's text truncations split surrogate pairs and show broken charactersAbiertocomponent:sight
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
agentic-os-org/ANOLISA#6738 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
bug Durable Agents Observability (AI Telemetry) status: needs triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
mastra-ai/mastra#26470 · 1 comentario ·
Los mantenedores suelen responder en 1 día