Explain it simply
State with useState Hook, in plain language
The useState hook is used to track local state within a component — every time the state value changes, it triggers a re-render of the component. React Native imports React's hooks system directly too, so the syntax is exactly the same as web React — the difference lies in UI events (onPress vs onClick) and components (TouchableOpacity vs button).
Problem → Why → Solution
The problem — why this concept matters
Trying to mutate state directly (isLiked = true) — you should always use the setIsLiked function
A common mistake
Calling the function immediately inside onPress (onPress={setIsLiked(!isLiked)}) — you need to wrap it in an arrow function (onPress={() => setIsLiked(!isLiked)})
Core concept — understand how it works
For State with useState Hook, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.
- JavaScript state
- native component and layout
- Android or iOS UI
Explain the React and native-platform behavior of State with useState Hook
Core syntax or pattern
import { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
export default function LikeButton() {
const [isLiked, setIsLiked] = useState(false);
return (
<TouchableOpacity onPress={() => setIsLiked(!isLiked)}>
<Text style={[styles.heart, isLiked && styles.liked]}>
{isLiked ? '❤️' : '🤍'}
</Text>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
heart: { fontSize: 32 },
liked: { transform: [{ scale: 1.2 }] },
});For State with useState Hook, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.
Read the code step by step
- JavaScript state
import { useState } from 'react';The useState hook is used to track local state within a component — every time the state value changes, it triggers a re-render of the component. React Native imports React's hooks system directly too, so the syntax is exactly the same as web React — the difference lies in UI events (onPress vs onClick) and components (TouchableOpacity vs button).
- native component and layout
</Text>For State with useState Hook, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.
- Android or iOS UI
});Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- JavaScript state
- native component and layout
- Android or iOS UI
Success
Explain the React and native-platform behavior of State with useState Hook
Failure
Trying to mutate state directly (isLiked = true) — you should always use the setIsLiked function
Let's think about this for a moment
The useState hook is used to track local state within a component — every time the state value changes, it triggers a re-render of the component. React Native imports React's hooks system directly too, so the syntax is exactly the same as web React — the difference lies in UI events (onPress vs onClick) and components (TouchableOpacity vs button).
Let's connect this to a real scenario
To build a like button, you create an isLiked state with useState(false), then call setIsLiked(!isLiked) inside TouchableOpacity's onPress — every press toggles the state and can change the icon color. You'll combine the style conditionally based on state (using the array syntax from the previous lesson).
Code Example
import { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
export default function LikeButton() {
const [isLiked, setIsLiked] = useState(false);
return (
<TouchableOpacity onPress={() => setIsLiked(!isLiked)}>
<Text style={[styles.heart, isLiked && styles.liked]}>
{isLiked ? '❤️' : '🤍'}
</Text>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
heart: { fontSize: 32 },
liked: { transform: [{ scale: 1.2 }] },
});Every time you press the heart icon, you'll see it toggle between filled and outline states.5-Minute Try-It
Build a counter app — create a count state with useState(0) and implement it so count increases every time you press the '+' button.
A Quick Heads-Up
Writing onPress={setIsLiked(!isLiked)} calls the function immediately on every render and can cause an infinite loop — you need to wrap it with an arrow function () => ...
Real-world usage
How to apply it in a real project
Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Mini challenge
Build it yourself
Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
import { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
export default function LikeButton() {
const [isLiked, setIsLiked] = useState(false);
return (
<TouchableOpacity onPress={() => setIsLiked(!isLiked)}>
<Text style={[styles.heart, isLiked && styles.liked]}>
{isLiked ? '❤️' : '🤍'}
</Text>
</TouchableOpacity>
);
}
const styles = StyleSheet.create({
heart: { fontSize: 32 },
liked: { transform: [{ scale: 1.2 }] },
});Expected: Explain the React and native-platform behavior of State with useState Hook
Show hint
Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- JavaScript state → native component and layout → Android or iOS UI
- For State with useState Hook, separate ownership of navigation, form, server, and device state. Test rapid taps, back gestures, deep links, process restarts, keyboard use, and screen-reader flows.
- Trying to mutate state directly (isLiked = true) — you should always use the setIsLiked function
- Test an original State with useState Hook example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
React Native Documentation — React Native