You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Suggestion]: lazy initialization example can encourage unsafe patterns - show example #8677
functionExpenseForm(){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.
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.
I agree that the distinction between pure initialization (like SuperCalculator) and creating resources with lifecycles (like subscriptions) during render is crucial. The current example could be misinterpreted. I think adding a warning or a specific example for lifecycle-managed resources would significantly improve clarity in the lazy initialization section.
I agree that the distinction between pure initialization (like SuperCalculator) and creating resources with lifecycles (like subscriptions) during render is crucial. The current example could be misinterpreted. I think adding a warning or a specific example for lifecycle-managed resources would significantly improve clarity in the lazy initialization section.
Thanks! The intention was to distinguish local initialization, such as SuperCalculator.initalizeIfNotReady() from lifecycle-managed resources like subscriptions. The problematic example is explicitly marked as something not to do during render, followed by Effect-based approach for setup and cleanup. I'll consider making this distinction more explicit.
Activity
karthik-awebon commentedon Oct 2, 2026
I agree that the distinction between pure initialization (like
SuperCalculator) and creating resources with lifecycles (like subscriptions) during render is crucial. The current example could be misinterpreted. I think adding a warning or a specific example for lifecycle-managed resources would significantly improve clarity in thelazy initializationsection.Mahendra-2006 commentedon Oct 3, 2026
Thanks! The intention was to distinguish local initialization, such as
SuperCalculator.initalizeIfNotReady()from lifecycle-managed resources like subscriptions. The problematic example is explicitly marked as something not to do during render, followed by Effect-based approach for setup and cleanup. I'll consider making this distinction more explicit.