Skip to main content
Albero Academy

Props & State

Pass data into components with props, remember changing data with useState, and understand what triggers a re-render.

Chapter 2React15 min readBeginner friendly
  • #React
  • #Props
  • #State

Components become useful when they're configurable and can remember things. Props pass data down into a component (like function arguments); state is data a component owns and can change over time. Getting the difference clear is the single biggest step to thinking in React.

Props: data in

Props are the object of attributes passed to a component. Read them from the function's parameter and use them in JSX:

passing props
function ProgramCard({ title, duration, price }) {
return (
<div className="card">
<h3>{title}</h3>
<p>{duration} · {price}</p>
</div>
);
}
// Used with attributes:
<ProgramCard title="Data Analytics" duration="6 months" price="₹65,000" />

The children prop

Whatever you nest between a component's tags arrives as the special children prop — perfect for wrappers like cards and layouts:

children
function Card({ children }) {
return <div className="card">{children}</div>;
}
<Card>
<h3>Placement support</h3>
<p>Included in every programme.</p>
</Card>

State with useState

State is data that changes and should update the screen when it does. The useState hook gives you a value and a setter:

a counter
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0); // initial value 0
return (
<button onClick={() => setCount(count + 1)}>
Enrolled: {count}
</button>
);
}

useState returns a pair: the current value and a function to update it. Calling the setter tells React the value changed, so it re-renders the component with the new value. Never assign to the state variable directly — always use the setter.

updating from the previous value
// When the next value depends on the previous, use the updater form:
setCount(prev => prev + 1);
// State can be any type
const [name, setName] = useState("");
const [items, setItems] = useState([]);
setItems(prev => [...prev, newItem]); // copy, then add — never mutate

What triggers a re-render

A component re-renders when its state changes or when its parent re-renders (passing new props). Two rules follow from this:

  • Treat state as immutable. Create a new array/object with spread rather than mutating — React compares references to decide what changed.
  • State updates are asynchronous & batched. Reading state right after setting it gives the old value; use the updater form when the next value depends on the current one.

Practice

Build a LikeButton with a count in state that increments on click, and a ProgramCard that takes title/price props. Then render a "cart" that stores an array of items in state and appends on a button click — using spread, not mutation.

Key takeaways

  1. 01Props pass data into a component and are read-only — they belong to the parent.
  2. 02children is the special prop holding whatever is nested between a component's tags.
  3. 03useState gives [value, setValue]; calling the setter re-renders with the new value.
  4. 04Treat state as immutable — copy arrays/objects with spread instead of mutating them.
  5. 05Use the updater form setX(prev => …) when the next value depends on the previous one.