[Suggestion]: lazy initialization example can encourage unsafe patterns - show example
維護者通常 4 天內回覆
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 82/100
- Issue 類型
- 文件
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- javascript, react
研究方向
從連結的 React 文件頁面中的「lazy initialization」部分開始。明確說明,當一次 render 未被提交時,在 render 期間建立由生命週期管理的資源可能不安全,並展示它與安全的 lazy initialization 之間的差異;當讀者能夠判斷哪些模式需要 Effect 時即完成。
由索引模型根據 Issue 內容生成。
描述
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
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 8k
- 平均合併
- 3 天 14 小時
- 30 天內合併 PR
- 8
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
reactjs/react.dev 的其他 Issue
-
[Suggestion]: Document that useFormStatus works with a preventDefault-ed onSubmit + startTransition可能已有人在做 @Felix-ming 於 3 天前認領。 未關閉type: documentation
難度 2/5 1-3 小時 新手友好度 78/100
reactjs/react.dev#8682 · 1 則留言 ·
維護者通常 4 天內回覆
-
type: documentation
難度 2/5 1-3 小時 新手友好度 72/100
reactjs/react.dev#8661 · 1 則留言 · 7 個 reaction ·
維護者通常 4 天內回覆
-
docs: improve useState explanation in quick start可能已有人在做 @tirthraj01 於 27 天前認領。 未關閉
難度 1/5 1 小時以內 新手友好度 82/100
維護者通常 4 天內回覆
-
[Bug]: Next button throws a runtime error instead of cycling to the first sculpture可能已有人在做 @Saurav10codes 於 35 天前認領。 未關閉bug: unconfirmed
難度 2/5 1-3 小時 新手友好度 74/100
reactjs/react.dev#8634 · 3 則留言 ·
維護者通常 4 天內回覆
-
[Typo]: Repeaded words in Pitfall section可能已有人在做 @mdimamhosen 於 37 天前認領。 未關閉type: typos
難度 1/5 1 小時以內 新手友好度 90/100
reactjs/react.dev#8622 · 1 則留言 ·
維護者通常 4 天內回覆
查看 reactjs/react.dev 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 65/100
processing/p5.sound.js#123 ·
-
難度 1/5 1-3 小時 新手友好度 82/100
PhilflowIO/dav-mcp#146 ·
維護者通常 1 天內回覆
-
good first issue hacktoberfest
難度 2/5 1-3 小時 新手友好度 82/100
Tanishq964/trail-kit.#4 ·
-
new icon permissions in review
難度 2/5 1-3 小時 新手友好度 62/100
simple-icons/simple-icons#15067 ·
維護者通常 1 天內回覆
-
status: needs triage
難度 2/5 1-3 小時 新手友好度 62/100
mastra-ai/mastra#26562 · 1 則留言 · 1 個 reaction ·
維護者通常 1 天內回覆