O
O
Oleg Gamega2017-01-23 20:32:07
React
Oleg Gamega, 2017-01-23 20:32:07

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

2 answer(s)
V
Vitaly, 2017-01-23
@vitali1995

The official documentation recommends using state and using refs only when there is no other way.

N
Nikita Gushchin, 2017-01-23
@iNikNik

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 question

Ask a Question

731 491 924 answers to any question