use-mounted.tsコメント追加
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 6
- Forks
- 2
- PR merge metrics
- No merged PRs in 30d
Description
調査結果をまとめます。このコードの useSyncExternalStore を使ったマウント判定パターンを直接的に「推奨」している単一の公式ドキュメントページは存在しませんでした。ただし、以下の複数の公式・準公式ソースが組み合わさって、このパターンの根拠となっています。
1. 最も近い公式リファレンス
useSyncExternalStore ドキュメント — getServerSnapshot の説明
https://react.dev/reference/react/useSyncExternalStore
第3引数 getServerSnapshot について、「サーバーレンダリング時とクライアントでのhydration時にのみ使用される」と明記されています。サーバーでは false、クライアントでは true を返すという useMounted のパターンは、この API 仕様の正当な活用です。
2. useEffect ドキュメントの「Displaying different content on the server and the client」セクション
公式ドキュメントでは useState + useEffect による didMount パターンが紹介されていますが、これは再レンダーのカスケードを引き起こすため、useMounted フックの JSDoc にある「No re-render cascade (unlike useState + useEffect pattern)」というコメントはこの欠点を指摘しています。
3. useLayoutEffect のトラブルシューティングセクション
ページ末尾に以下の記述があります:
"If you synchronize your component with an external data store and rely on useLayoutEffect for different reasons than measuring layout, consider useSyncExternalStore instead which supports server rendering."
これはSSR対応のためのフォールバックとして useSyncExternalStore を公式に推奨しています。
4. React GitHub Issue #23068(準公式)
https://github.com/facebook/react/issues/23068
このissueで React チームメンバーが直接コメントしています:
-
sebmarkbage(Sebastian Markbåge, React コア開発者):「useSyncExternalStore is the API suggested to deal with external mutable state. That API has an SSR option which is where you're supposed to return the value that should be used on the server and the value that should be used during hydration.」
-
gaearon(Dan Abramov):「I think useSyncExternalStore should work for this?」
さらに同issue内で shredor というユーザーが、useMounted とまったく同じパターンのコードを投稿しています(getClientSnapshot = () => true, getServerSnapshot = () => false)。
結論
コード内のJSDocにある「This pattern is recommended by React team」という記述は、単一の公式ドキュメントページから来たものではなく、上記の複数ソース(特にGitHub Issue #23068でのReactチームメンバーの発言)を総合した解釈です。公式ドキュメントが直接「マウント判定には useSyncExternalStore を使え」と書いているわけではありませんが、API設計の意図とReactチームのコメントから見て、このパターンは妥当なアプローチと言えます。
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating use-mounted.ts and read its existing JSDoc alongside the React useSyncExternalStore documentation and Issue #23068 referenced here. Update the comment to accurately describe the pattern and its supporting sources, avoiding a stronger recommendation than the evidence supports; done means the rationale and links are clear in the file.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100