Answer the question
In order to leave comments, you need to log in
Refs vs state, which is better and when?
Hello.
Not a holivar for the sake of what is better and where.
For example, there is an input field, what is better to hang a listener on it, and write its data to the state or make direct access to it?
And in what cases is it better to use ref in which state?
Thanks in advance.
Answer the question
In order to leave comments, you need to log in
The official documentation recommends using state and using refs only when there is no other way.
ref is used when a DOM node or react element is needed. For example, after some event you want to put focus in some input. Take the ref of the input and set the focus.
In state - the local state is stored.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question