State and props control what a component shows and how it acts. They help the app update and respond to user actions.
Why state and props drive component behavior in React Native
Introduction
Syntax
React Native
import React, { useState } from 'react'; import { View, Text, Button } from 'react-native'; function MyComponent(props) { const [stateValue, setStateValue] = useState(initialValue); return ( <View> <Text>{props.someProp}</Text> <Button onPress={() => setStateValue(newValue)} title="Press me" /> </View> ); }
props are values passed from a parent component to a child component.
state is data managed inside the component that can change over time.
Examples
props to show a name passed from its parent.React Native
import React from 'react'; import { Text } from 'react-native'; function Greeting(props) { return <Text>Hello, {props.name}!</Text>; }
state to keep track of a number that changes when the button is pressed.React Native
import React, { useState } from 'react'; import { View, Text, Button } from 'react-native'; function Counter() { const [count, setCount] = useState(0); return ( <View> <Text>Count: {count}</Text> <Button onPress={() => setCount(count + 1)} title="Add" /> </View> ); }
Sample App
This app shows text that changes color when you press the button. The color is stored in state. It also shows a message passed from the parent using props.
React Native
import React, { useState } from 'react'; import { View, Text, Button } from 'react-native'; export default function ColorChanger(props) { const [color, setColor] = useState('blue'); return ( <View style={{ padding: 20, alignItems: 'center' }}> <Text style={{ color: color, fontSize: 24 }}>This text changes color!</Text> <Button title="Change to red" onPress={() => setColor('red')} accessibilityLabel="Change text color to red" /> <Text style={{ marginTop: 10 }}>Parent says: {props.message}</Text> </View> ); }
Important Notes
Changing state causes the component to update and show new data.
props are read-only inside the component; you cannot change them there.
Use state for data that changes inside the component, and props for data passed from outside.
Summary
State holds data that can change inside a component.
Props pass data from parent to child components.
Together, they control what the component shows and how it behaves.
Practice
1. What is the main role of
state in a React Native component?easy
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]
Hint: State holds changing data inside component [OK]
Common Mistakes:
- Confusing state with props
- Thinking state is for styling
- Believing state is static
2. Which of the following is the correct way to define a state variable using React hooks in a functional component?
easy
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]
Hint: useState returns [value, setter] array [OK]
Common Mistakes:
- Not destructuring useState result
- Using var or let instead of const
- Assigning useState directly to a variable
3. Consider this React Native component code:
What will be displayed on the screen?
function Greeting(props) {
return Hello, {props.name}!;
}
function App() {
return ;
}What will be displayed on the screen?
medium
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]
Hint: Props pass data from parent to child [OK]
Common Mistakes:
- Assuming props is undefined
- Ignoring the passed name value
- Confusing props with state
4. What is wrong with this React Native code snippet?
function Counter() {
const [count, setCount] = useState(0);
return (
{count}
count = count + 1} />
);
}medium
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]
Hint: Always use setter to update state, never assign directly [OK]
Common Mistakes:
- Trying to change state variable directly
- Forgetting to import useState
- Misusing Button props
5. You want a component to show a message that changes when a button is pressed. Which approach correctly uses state and props to achieve this?
function Message({ initial }) {
const [text, setText] = useState(initial);
return (
{text}
setText('Updated!')} />
);
}
function App() {
return ;
}hard
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]
Hint: Use props for initial data, state for changes [OK]
Common Mistakes:
- Thinking props can change after initial render
- Trying to update props directly
- Assuming Button can't trigger state changes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
