------------------- | Counter App | | | | 0 | | | | [+] [Reset] | -------------------
Why state and props drive component behavior in React Native - Build It to Prove It
import React, { useState } from 'react'; import { View, Text, Button, StyleSheet } from 'react-native'; function NumberDisplay(props) { // TODO: Show the number passed via props return ( <Text style={styles.numberText}>0</Text> ); } export default function CounterScreen() { // TODO: Create state for the number return ( <View style={styles.container}> <Text style={styles.title}>Counter App</Text> {/* TODO: Use NumberDisplay and pass the number as prop */} <View style={styles.buttons}> {/* TODO: Add '+' button to increase number */} {/* TODO: Add 'Reset' button to reset number to 0 */} </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center' }, title: { fontSize: 24, marginBottom: 20 }, numberText: { fontSize: 48, marginBottom: 20 }, buttons: { flexDirection: 'row', justifyContent: 'space-between', width: 200 } });
import React, { useState } from 'react'; import { View, Text, Button, StyleSheet } from 'react-native'; function NumberDisplay(props) { return ( <Text style={styles.numberText}>{props.number}</Text> ); } export default function CounterScreen() { const [count, setCount] = useState(0); return ( <View style={styles.container}> <Text style={styles.title}>Counter App</Text> <NumberDisplay number={count} /> <View style={styles.buttons}> <Button title="+" onPress={() => setCount(count + 1)} /> <Button title="Reset" onPress={() => setCount(0)} /> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center' }, title: { fontSize: 24, marginBottom: 20 }, numberText: { fontSize: 48, marginBottom: 20 }, buttons: { flexDirection: 'row', justifyContent: 'space-between', width: 200 } });
This app uses state to keep track of the current number shown on screen. The count variable is created with useState(0), starting at zero.
The NumberDisplay component receives the current number as a prop called number. This means it shows whatever value the parent CounterScreen gives it.
When the '+' button is pressed, the setCount function updates the state by adding 1. React then re-renders the screen, passing the new number down to NumberDisplay, so the displayed number changes.
The 'Reset' button sets the count back to zero, showing how changing state updates the UI automatically.
This example shows how state holds data that can change inside a component, and props pass data from parent to child components, driving what the user sees and how the app behaves.
------------------- | Counter App | | | | 0 | | | | [+] [Reset] | -------------------
Practice
state in a React Native component?Solution
Step 1: Understand what state represents
State is used to store data that can change during the life of a component, like user input or toggles.Step 2: Differentiate state from props
Props are for passing data from parent to child, not for internal changes.Final Answer:
To hold data that can change over time inside the component -> Option DQuick Check:
State = internal changing data [OK]
- Confusing state with props
- Thinking state is for styling
- Believing state is static
Solution
Step 1: Recall React hook syntax for state
useState returns an array with the current value and a function to update it, usually destructured.Step 2: Check each option
const [count, setCount] = useState(0); correctly uses array destructuring with useState. Others misuse syntax or do not destructure.Final Answer:
const [count, setCount] = useState(0); -> Option CQuick Check:
useState with destructuring = correct [OK]
- Not destructuring useState result
- Using var or let instead of const
- Assigning useState directly to a variable
function Greeting(props) {
return Hello, {props.name}!;
}
function App() {
return ;
}What will be displayed on the screen?
Solution
Step 1: Understand how props work in this example
TheGreetingcomponent receivesnameas a prop fromAppwith value "Alice".Step 2: Check the rendered output
The component returnsHello, {props.name}!which becomes "Hello, Alice!" on screen.Final Answer:
Hello, Alice! -> Option AQuick Check:
Props pass data = Hello, Alice! [OK]
- Assuming props is undefined
- Ignoring the passed name value
- Confusing props with state
function Counter() {
const [count, setCount] = useState(0);
return (
{count}
count = count + 1} />
);
}Solution
Step 1: Identify how state updates should happen
State variables are read-only; to change state, use the setter function likesetCount.Step 2: Check the onPress handler
The code tries to assign directly tocount, which is invalid and won't update the UI.Final Answer:
You cannot assign directly to the state variable; must use setCount -> Option AQuick Check:
State updates require setter function [OK]
- Trying to change state variable directly
- Forgetting to import useState
- Misusing Button props
function Message({ initial }) {
const [text, setText] = useState(initial);
return (
{text}
setText('Updated!')} />
);
}
function App() {
return ;
}Solution
Step 1: Analyze how props and state are used
The component receives an initial message via props and stores it in state for changes.Step 2: Check the button's onPress behavior
Pressing the button callssetTextto update the state, which updates the displayed message.Final Answer:
This works because state holds the message and props provide the initial value -> Option BQuick Check:
Props initialize state; state changes update UI [OK]
- Thinking props can change after initial render
- Trying to update props directly
- Assuming Button can't trigger state changes
Ready to prove it?
Pass the test and this pattern moves unknown
