Discover how React's state and props save you from messy, error-prone code and make your app smart and responsive!
State vs props comparison in React - When to Use Which
Imagine building a web app where you have to manually update every piece of information on the screen whenever something changes, like user input or data from a server.
You try to pass data between parts of your app by copying and pasting values everywhere.
This manual way is confusing and slow. You might forget to update some parts, causing bugs.
It's hard to keep track of where data came from or who can change it.
React's state and props help organize data flow clearly.
State holds data that can change inside a component.
Props pass data from parent to child components safely and read-only.
This makes your app easier to build, understand, and fix.
function updateName(name) {
document.getElementById('name').textContent = name;
document.getElementById('greeting').textContent = 'Hello, ' + name;
}import React, { useState } from 'react'; function Greeting({ name }) { return <h1>Hello, {name}</h1>; } function App() { const [name, setName] = useState('Alice'); return <Greeting name={name} />; }
You can build interactive apps where data flows clearly and updates automatically when things change.
Think of a shopping cart: state tracks items you add or remove, while props pass the cart details to different parts of the page to show totals or item lists.
State holds data that can change inside a component.
Props pass data from parent to child components and cannot be changed by the child.
Using state and props keeps your app organized and easy to update.
Practice
state and props in React?Solution
Step 1: Understand what
stateisStateis data managed inside a component that can change over time.Step 2: Understand what
propsarePropsare data passed from a parent component to a child component and cannot be changed by the child.Final Answer:
Stateis internal and can change;propsare external and read-only. -> Option BQuick Check:
State changes internally, props are read-only from parent [OK]
- Thinking props can be changed inside the child component
- Confusing state as data passed from parent
- Believing props hold styling only
title with value "Hello" to a child component Header?Solution
Step 1: Recall JSX syntax for passing props
Props are passed as attributes inside the component tag, like HTML attributes.Step 2: Check correct syntax for string prop
Use quotes around string values:title="Hello".Final Answer:
<Header title="Hello" /> -> Option AQuick Check:
Props use attribute syntax with quotes for strings [OK]
- Using curly braces without variable for string
- Using object-like syntax inside JSX tag
- Using colon instead of equals sign
function Counter({ initial }) {
const [count, setCount] = React.useState(initial);
return <div>Count: {count}</div>;
}
// Usage:
<Counter initial={5} />What will be displayed when this component renders?
Solution
Step 1: Understand prop usage
The propinitialis passed with value 5 to theCountercomponent.Step 2: Understand state initialization
The statecountis initialized withinitial, socountstarts at 5.Final Answer:
Count: 5 -> Option CQuick Check:
State initialized from prop value 5 [OK]
- Assuming state starts at 0 by default
- Printing the prop name instead of value
- Confusing undefined with missing prop
function Message(props) {
props.text = "Hello";
return <div>{props.text}</div>;
}Solution
Step 1: Understand props immutability
Props are read-only and cannot be changed inside the component.Step 2: Identify the error in code
Assigningprops.text = "Hello"tries to modify props, which is not allowed.Final Answer:
You cannot assign a new value to props inside a component. -> Option AQuick Check:
Props are immutable inside components [OK]
- Trying to change props directly
- Confusing props with state
- Thinking only class components can modify props
state and 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 DQuick Check:
State changes internally; props stay read-only [OK]
- Trying to change props directly
- Using props to hold changing data
- Passing updated data from child to parent incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
