← Back to React, page 2

react-0021 · Easy fresher

What is the difference between props and state in React?

Props are inputs passed from a parent to a child. The child reads them and must not change them. State is data a component owns and can change over time, and changing it triggers a re-render. A value that comes from a parent is a prop. A value that the component itself must remember and update is state. Data flows down through props, and changes flow up through callback props.

Full answer

Think of a marksheet row. The student name and marks arrive from the page that holds the data. The row cannot change them, it only displays them. Those are props. Whether the row is expanded to show details is something the row itself remembers. That is state.

Props are read-only inputs. A parent writes them like attributes: <StudentRow name="Asha" marks={82} />. The child receives an object, usually destructured in the function signature. If a child needs the parent to change something, the parent passes a function as a prop, and the child calls it. This is how data flows down and events flow up.

State is private memory. It is created with useState, which returns the current value and a setter. Calling the setter schedules a re-render with the new value. State survives between renders, while ordinary local variables are recreated every time the function runs.

Both props and state can cause a component to re-render, and both are snapshots for one render. Neither may be changed directly: never write props.name = 'x' or count++ on state.

A common design question is whether a value should be state at all. If it can be calculated from props or other state, calculate it during render instead of storing a copy. For example, a full name from first and last name needs no state. Duplicated state drifts out of sync.

Another sign for choosing: if two components need the same state, move it to their nearest common parent and pass it down as props. That pattern is called lifting state up.

jsx
import { useState } from 'react';

function StudentRow({ name, marks }) {
  const [expanded, setExpanded] = useState(false);
  const grade = marks >= 75 ? 'A' : marks >= 50 ? 'B' : 'C'; // derived, not state

  return (
    <li>
      {name}
      <button onClick={() => setExpanded(!expanded)}>{expanded ? 'Hide' : 'Show'} details</button>
      {expanded && <span> Marks: {marks}, Grade: {grade}</span>}
    </li>
  );
}

export default function App() {
  return (
    <ul>
      <StudentRow name="Asha" marks={82} />
      <StudentRow name="Ravi" marks={47} />
    </ul>
  );
}

name and marks are props, expanded is state, and the grade is calculated during render. The check clicked both buttons. Rendered result: <ul><li>Asha<button>Hide details</button><span> Marks: 82, Grade: A</span></li><li>Ravi<button>Hide details</button><span> Marks: 47, Grade: C</span></li></ul>

Key points

  • Props come from the parent and are read-only. State is owned by the component and changes with a setter.
  • Changing state re-renders the component. A parent re-rendering with new props also re-renders the child.
  • Never mutate props or state directly.
  • Do not store values that can be derived from props or other state.
  • Children ask parents to change data by calling callback props.

Common mistakes

  • Trying to modify a prop inside the child.
  • Copying a prop into state "to edit it" and then missing later updates from the parent.
  • Storing derived values in state.
  • Mutating state objects directly instead of using the setter.

Follow-ups

  • What happens if you copy a prop into useState, and how do you avoid the problem?
  • How does a child tell its parent that something happened?
  • Where would you keep state that two sibling components both need?

Tips

  • Give a one-line definition for each and then a real example. Do not stop at "props are immutable".
  • Mention derived values. It shows design sense.
  • Use "owns" for state and "receives" for props.