Lesson 3 of 9
useRef - Focus, Layout & Imperatives
useRef in React Native is more versatile than in web: it manages TextInput focus, scroll position, animation values, interval IDs, and component measurements - all without causing re-renders.
useRef Common Mobile Patterns
import { useRef, useEffect } from 'react';
import { TextInput, FlatList, View, Animated } from 'react-native';
// ---- Focus management between inputs ----
function Form() {
const emailRef = useRef<TextInput>(null);
const passwordRef = useRef<TextInput>(null);
useEffect(() => {
// Auto-focus first field on mount
const timer = setTimeout(() => emailRef.current?.focus(), 100);
return () => clearTimeout(timer);
}, []);
return (
<>
<TextInput ref={emailRef} returnKeyType="next" onSubmitEditing={() => passwordRef.current?.focus()} />
<TextInput ref={passwordRef} returnKeyType="done" secureTextEntry />
</>
);
}
// ---- Scroll to position (FlatList) ----
function Feed({ data }: { data: unknown[] }) {
const listRef = useRef<FlatList>(null);
function scrollToTop() {
listRef.current?.scrollToOffset({ offset: 0, animated: true });
}
function scrollToItem(index: number) {
listRef.current?.scrollToIndex({ index, animated: true, viewPosition: 0 });
}
return <FlatList ref={listRef} data={data} renderItem={() => null} keyExtractor={(_, i) => String(i)} />;
}
// ---- Measure layout (for positioning tooltips, popovers) ----
function MeasuredButton({ onMeasured }: { onMeasured: (layout: { x: number; y: number; width: number; height: number }) => void }) {
const ref = useRef<View>(null);
function measure() {
ref.current?.measureInWindow((x, y, width, height) => {
onMeasured({ x, y, width, height });
});
}
return <View ref={ref} onLayout={measure}><Text>Measure me</Text></View>;
}
// ---- Stable value across renders (no re-render trigger) ----
function PollingComponent() {
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
useEffect(() => {
intervalRef.current = setInterval(fetchData, 5000);
return () => {
if (intervalRef.current) clearInterval(intervalRef.current);
};
}, []);
}
async function fetchData() {}
function Text({ children }: { children: string }) { return null; }Tip
Tip
Practice useRef Focus Layout Imperatives in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Accessing native methods like focus(), scrollToIndex(), and measureInWindow()
Practice Task
Note
Practice Task - (1) Write a working example of useRef Focus Layout Imperatives from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with useRef Focus Layout Imperatives is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?