The useState hook helps you keep track of changing information in your app. It lets your app remember things like user input or button clicks.
useState hook in React Native
const [stateVariable, setStateFunction] = useState(initialValue);
stateVariable holds the current value.
setStateFunction is used to change the value and update the screen.
const [count, setCount] = useState(0);const [name, setName] = useState('');const [isVisible, setIsVisible] = useState(true);
This simple app shows a number starting at 0. Each time you press the button, the number goes up by 1. The useState hook remembers the number and updates the screen.
import React, { useState } from 'react'; import { View, Text, Button } from 'react-native'; export default function Counter() { const [count, setCount] = useState(0); return ( <View style={{ padding: 20, alignItems: 'center' }}> <Text style={{ fontSize: 24, marginBottom: 10 }}>Count: {count}</Text> <Button title="Increase" onPress={() => setCount(count + 1)} /> </View> ); }
Always use the setter function (like setCount) to change state. Changing the variable directly won't update the screen.
State updates are asynchronous, so the new value appears after React processes the change.
You can use multiple useState hooks to track different pieces of information.
useState lets your app remember and update values.
Use it to handle user actions like clicks or typing.
Always update state with the setter function to refresh the screen.
Practice
useState hook do in a React Native component?Solution
Step 1: Understand the purpose of useState
TheuseStatehook is designed to let components keep track of values that can change, like user input or toggles.Step 2: Compare options with useState's role
Only It allows the component to remember and update values over time. correctly describes this behavior. Styling, fetching data, or navigation are handled by other tools.Final Answer:
It allows the component to remember and update values over time. -> Option CQuick Check:
useState manages state = A [OK]
- Confusing useState with styling or navigation
- Thinking useState fetches data automatically
- Believing useState creates new screens
count with initial value 0 using useState?Solution
Step 1: Recall useState syntax
The correct syntax uses array destructuring:const [state, setState] = useState(initialValue);Step 2: Check each option
const [count, setCount] = useState(0); matches the correct syntax. const count = useState(0); misses destructuring, C uses comma incorrectly, and A misses parentheses.Final Answer:
const [count, setCount] = useState(0); -> Option BQuick Check:
useState syntax = D [OK]
- Not using array destructuring
- Forgetting parentheses after useState
- Using commas instead of brackets
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';
export default function Counter() {
const [count, setCount] = useState(0);
return (
{count}
setCount(count + 1)} />
);
}Solution
Step 1: Understand initial state and button action
The initial count is 0. Each button press callssetCount(count + 1), increasing count by 1.Step 2: Calculate count after two presses
After first press: count = 1. After second press: count = 2. The displayed text updates accordingly.Final Answer:
2 -> Option AQuick Check:
Two increments from 0 = 2 [OK]
- Thinking count stays 0 without update
- Confusing initial value with updated value
- Expecting error due to misunderstanding setCount
useState:import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';
export default function Example() {
const [value, setValue] = useState;
return (
{value}
setValue('Hello')} />
);
}Solution
Step 1: Check useState declaration
The code usesuseStatewithout parentheses, so it does not call the function to get state and setter.Step 2: Understand consequences
Without callinguseState(),valueandsetValueare undefined, causing errors when used.Final Answer:
useState is missing parentheses to call it as a function. -> Option AQuick Check:
useState() must be called with () [OK]
- Forgetting parentheses after useState
- Misnaming setter function
- Assuming Button lacks onPress prop
isVisible between true and false each time a button is pressed. Which is the best way to update the state using useState to ensure the toggle works correctly even if updates happen quickly?const [isVisible, setIsVisible] = useState(false);
Solution
Step 1: Understand state updates with previous value
When toggling, using the previous state ensures correct flipping even if updates queue quickly.Step 2: Compare update methods
setIsVisible(prev => !prev); uses a function to get previous state and invert it, which is reliable. setIsVisible(!isVisible); uses current state which may be stale. Options B and D set fixed values, not toggling.Final Answer:
setIsVisible(prev => !prev); -> Option DQuick Check:
Use functional update for toggling state [OK]
- Using current state directly causing stale updates
- Setting fixed true or false instead of toggling
- Not using functional update for quick repeated changes
Ready to prove it?
Pass the test and this pattern moves unknown
