[Suggestion]: lazy initialization example can encourage unsafe patterns - show example
メンテナーはふだん 4 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 82/100
- issue の種類
- ドキュメント
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- javascript, react
調査の方向性
リンク先のReactドキュメントページにある「lazy initialization」セクションから始めてください。レンダーがコミットされない場合、レンダー中にライフサイクルで管理されるリソースを作成するのは安全でない可能性があることを明確にし、安全な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時間
- マージ済み PR(30日)
- 8
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 件 ·
メンテナーはふだん 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
-
[BUG] Multi-day events show "Ended" while still in progress対応中かも @tarunagnihotri534 が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
data-umbrella/du-event-board#231 · コメント 2 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
NaturalIntelligence/fast-xml-parser#888 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 80/100
USACE/chart-docs#766 ·
-
bug callouts regression revealjs
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
quarto-dev/quarto-cli#15014 ·
メンテナーはふだん 1 日以内に返信