Great to know that useLayoutEffect &useRef go hand in hand π
And regarding useRef, may I ask why using getElementById or querySelector is "not an ideal solution in React" (reference: When to use useRef) and how using useRef is different from using them?
Overall itβs more inline with the rest of your react code.
1 problem with getElementById is that you can have multiples of the same ID. So if you have an input component with an ID of βfooβ, then that will display on the output multiple times.
Thanks Ruben for the post.
Great to know that
useLayoutEffect
&useRef
go hand in hand πAnd regarding
useRef
, may I ask why usinggetElementById
orquerySelector
is "not an ideal solution in React" (reference: When to use useRef) and how usinguseRef
is different from using them?Great question.
Overall itβs more inline with the rest of your react code.
1 problem with getElementById is that you can have multiples of the same ID. So if you have an input component with an ID of βfooβ, then that will display on the output multiple times.
Great example here.
Another advantage is that you can toss the reference to another component, which helps maintain your unidirectional data flow.
I can clearly see why now π.
Thank you, Ruben π