DEV Community

Agata
Agata

Posted on

React.useRefの理解を含めるエクササイズ

document.getElementById vs useRef vs useState

違いはわかりますか…?
いつ使えばいいか迷ったら、ぜひ下記のエクササイズをやってみてください!

エクササイズはこちらです:

https://codesandbox.io/s/react-useref-t7pmkn?file=/read.me

プロジェクトでは4つエクササイズがあります:

  • エクササイズ1:src/components/Zero.tsx (document.getElementById vs useState)
  • エクササイズ2:src/components/First.tsx (document.getElementById vs useRef)
  • エクササイズ3:src/components/Second.tsx (useRef vs useState:useRefを使いすぎた場合)
  • エクササイズ4:src/components/Third.tsx (useRef vs useState:不要なレンダリング防ぎ方)

read.meを参考にしてください。

回答はこちらになります:

https://codesandbox.io/s/react-hooks-good-answers-6mhjcz?file=/read.me

Top comments (0)