Grokking React Native
Vote

0% completed

​

Creating and Using Refs

How can a button press put the cursor inside a text field? A TextInput has no prop that means "receive the focus now". It does have a focus method, so your code needs a way to reach the field and call that method. The useRef Hook provides that way, and this chapter builds a small message screen with it.

Creating a Ref

You call useRef at the top of a component and give it a starting value:

const refName = useRef(initialValue);

The result is an object with a single property named current, and current starts as the value you passed

.....

.....

.....

Like the course? Get enrolled and start learning!

Reading Progress

0%


Vote for new content