State and props help React components manage and share data. State is for data that changes inside a component. Props are for passing data from parent to child components.
When a component needs to remember user input or interaction.
When you want to send information from a parent component to a child component.
When a component's data changes over time and affects what it shows.
When you want to keep some data private inside a component.
When you want to reuse a component with different data.
State is created inside a component using useState hook.
Props are passed to components like HTML attributes.
Examples
This creates a state variable count starting at 0.
React
const [count, setCount] = React.useState(0);
This passes a prop called name with value "Alice" to the Greeting component.
React
<Greeting name="Alice" />
This component uses the name prop to show a greeting.
React
function Greeting(props) {
return <h1>Hello, {props.name}!</h1>;
}
Sample Program
This example shows a Counter component with state count. The button changes the state. The Message child component receives the count as a prop and displays it.
What will be displayed when this component renders?
medium
A. Count: 0
B. Count: undefined
C. Count: 5
D. Count: initial
Solution
Step 1: Understand prop usage
The prop initial is passed with value 5 to the Counter component.
Step 2: Understand state initialization
The state count is initialized with initial, so count starts at 5.
Final Answer:
Count: 5 -> Option C
Quick Check:
State initialized from prop value 5 [OK]
Hint: State starts with prop value passed in [OK]
Common Mistakes:
Assuming state starts at 0 by default
Printing the prop name instead of value
Confusing undefined with missing prop
4. What is wrong with this React component code?
function Message(props) {
props.text = "Hello";
return <div>{props.text}</div>;
}
medium
A. You cannot assign a new value to props inside a component.
B. The return statement is missing parentheses.
C. The component should use state instead of props.
D. The component must be a class to modify props.
Solution
Step 1: Understand props immutability
Props are read-only and cannot be changed inside the component.
Step 2: Identify the error in code
Assigning props.text = "Hello" tries to modify props, which is not allowed.
Final Answer:
You cannot assign a new value to props inside a component. -> Option A
Quick Check:
Props are immutable inside components [OK]
Hint: Props are read-only; never assign to them [OK]
Common Mistakes:
Trying to change props directly
Confusing props with state
Thinking only class components can modify props
5. You want a component to display a counter that starts from a value passed by its parent and can be incremented by the user. Which approach correctly uses state and props?
hard
A. Modify the prop value directly when user clicks increment.
B. Keep counter value only in props and pass updated props from child to parent.
C. Use props only and update them inside the component.
D. Initialize state with the prop value; update state on user action; do not modify props.
Solution
Step 1: Understand data flow
Props provide initial data from parent; state holds data that changes inside the component.
Step 2: Apply correct usage
Initialize state with prop value, then update state on user actions. Props remain unchanged.
Final Answer:
Initialize state with the prop value; update state on user action; do not modify props. -> Option D
Quick Check:
State changes internally; props stay read-only [OK]
Hint: Use props for initial value, state for changes [OK]
Common Mistakes:
Trying to change props directly
Using props to hold changing data
Passing updated data from child to parent incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.