useRef in react native.
In React Native, useRef is a hook that allows you to create references to components or values that persist across renders without causing the component to re-render when they are updated. It is useful for handling direct interactions with components and managing mutable data that doesn't affect the component's visual output.
Key Features of useRef in React Native:
Component References:
Storing Mutable Values:
Persistent Across Renders:
Syntax:
const reference = useRef(initialValue);
reference is an object with a .current property, which holds the value or component reference. You can read or update this .current value as needed.
Example 1: Focusing a TextInput
In React Native, you can use useRef to interact with a TextInput component programmatically. For example, you might want to focus an input field when a button is pressed.
import React, { useRef } from 'react';
import { View, TextInput, Button } from 'react-native';
export default function App() {
const inputRef = useRef(null);
const handleFocus = () => {
inputRef.current.focus(); // Focuses the TextInput when the button is pressed
};
return (
<View>
<TextInput ref={inputRef} placeholder="Enter text here" />
<Button title="Focus Input" onPress={handleFocus} />
</View>
);
}
In this example, useRef creates a reference to the TextInput component. When the button is pressed, the input field is focused by calling the focus() method on the TextInput.
Example 2: Storing Mutable Values
useRef can also store values that need to be updated across renders but don’t need to trigger the UI to re-render. This is helpful in cases like managing counters or timers where the changes don’t immediately affect the UI.
Recommended by LinkedIn
import React, { useRef, useState } from 'react';
import { View, Text, Button } from 'react-native';
export default function TimerApp() {
const [count, setCount] = useState(0); // For triggering re-renders
const countRef = useRef(count); // For storing mutable value
const incrementWithoutRerender = () => {
countRef.current += 1; // Updates ref without causing re-render
console.log(countRef.current); // Logs the updated value
};
const incrementWithRerender = () => {
setCount(count + 1); // Updates state and triggers re-render
};
return (
<View>
<Text>Count: {count}</Text>
<Button title="Increment Without Re-render" onPress={incrementWithoutRerender} />
<Button title="Increment With Re-render" onPress={incrementWithRerender} />
</View>
);
}
countRef holds a mutable count value that persists between renders. Pressing the first button updates countRef.current without triggering a re-render. The second button uses state (useState), causing the component to re-render when the count changes.
Example 3: Controlling a ScrollView
You can use useRef to control the scroll position of a ScrollView in React Native. For example, you can scroll to the end of the content when a button is pressed.
import React, { useRef } from 'react';
import { ScrollView, Text, Button, View } from 'react-native';
export default function ScrollViewExample() {
const scrollViewRef = useRef(null);
const scrollToEnd = () => {
scrollViewRef.current.scrollToEnd({ animated: true }); // Scrolls to the bottom of the ScrollView
};
return (
<View>
<ScrollView ref={scrollViewRef} style={{ height: 200 }}>
<Text>Some long content...</Text>
<Text>More content...</Text>
</ScrollView>
<Button title="Scroll to End" onPress={scrollToEnd} />
</View>
);
}
scrollViewRef is a reference to the ScrollView. By calling scrollToEnd(), you can programmatically scroll to the end of the content when the button is pressed.
Important Considerations in React Native:
Component Interaction:
Managing Data Without Re-renders:
Persistence Across Renders:
Conclusion:
In React Native, useRef is used for referencing components or storing mutable values that persist across renders without causing re-renders. It allows you to interact with component methods and handle internal logic that doesn't impact the UI directly, making it a powerful tool for managing interactions and performance optimizations.
Very informative