[Suggestion]: How to avoid null checks when initializing useRef later
まだ誰も着手していません。
- 主要言語
- JavaScript
- スター
- 11.8k
- フォーク
- 7.9k
- 平均マージ
- 1日 11時間
- マージ済み PR(30日)
- 11
説明
Summary
The example is very bad.
Page
https://react.dev/reference/react/useRef#avoiding-recreating-the-ref-contents
Details
The solution how to avoid this pitfall doesn't seem very robust. If for any reason you wanted to instantiate to VideoPlayer but you want your ref to generally be of type VideoPlayer | null you will never be able to put a null in there.
A more robust way actually seems to leverage useState via:
const [playerRef] = useState(()=>({current:new VideoPlayer()}))
While investigating this issue i've concluded that people in general misunderstand what useRef is, going to the extremes of thinking it's "just for dom elements". playerRef in this snipper is exactly MutableRefObject<VideoPlayer> and this approach doesn't involve all kinds of acrobatics, like lying to a type checker, checks inside the render body etc.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンク先の「avoiding recreating the ref contents」セクションにある useRef のドキュメントを読み、現在の例と、そこで示されている型付けの前提を確認してください。提案されている useState アプローチを既存のガイダンスと比較し、意図する ref の型、null の挙動、初期化に伴うトレードオフが明確になるように、例または説明を更新してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, react
- 領域
- documentation
- issue の種類
- ドキュメント
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100