React: useState or useRef?
Solution 1:
The main difference between both is :
useState
causes re-render, useRef
does not.
The common between them is, both useState
and useRef
can remember their data after re-renders. So if your variable is something that decides a view layer render, go with useState
. Else use useRef
I would suggest reading this article.
Solution 2:
Basically, We use UseState in those cases, in which the value of state should be updated with re-rendering.
when you want your information persists for the lifetime of the component you will go with UseRef because it's just not for work with re-rendering.