Skip to the solution
EasyFrontend5 min read

What is the difference between state and props?

196 views

The problem

Question presented to candidate: "What is the difference between state and props, and how do you decide which one a value should be?"

What a strong answer should cover:

  • Props are inputs passed in by the parent; state is data the component owns and can change itself.
  • Props are read-only; state is changed through a setter, never by assignment.
  • Both cause a re-render when they change, so that is not the distinction.
  • State persists across re-renders and is tied to the component's position in the tree β€” a prop change does not reset it, but a key change remounts the component and does.
  • The deciding question: can this component change the value itself? If yes it is state; if it comes from above it is a prop.
  • Do not copy props into state β€” it creates a second source of truth that stops tracking the first.
  • A value that can be derived from props or state should be neither; compute it during render.
  • Where the value lives is a design decision: shared values get lifted, local ones stay colocated.

Clarifying questions expected:

  • "Is this value something the component changes, or is it given to it?"
  • "Does anything else need the same value?" β€” that pushes toward lifting.

Code / implementation expected: Optional. A component taking a prop and holding its own state alongside is enough.

  • state
  • props
  • components
  • data flow

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Try it in the playground

Open any example in the playground to run and change it.

ReactState surviving a prop change, and resetting via key
import { useState } from "react";

// Takes a prop (read-only) AND owns state (its own). Both re-render it; only
// one of them can it change.
function Counter({ label }) {
  const [count, setCount] = useState(0);
  return (
    <div style={{ border: "1px solid #ddd", borderRadius: 8, padding: 12, marginBottom: 10 }}>
      <p style={{ margin: "0 0 6px" }}>
        prop <code>label</code>: <strong>{label}</strong> Β· state <code>count</code>:{" "}
        <strong>{count}</strong>
      </p>
      <button onClick={() => setCount((c) => c + 1)}>+1</button>
    </div>
  );
}

// A derived value is NEITHER prop nor state β€” just compute it.
function Greeting({ firstName, lastName }) {
  const fullName = firstName + " " + lastName;   // no useState needed
  const initials = (firstName[0] + lastName[0]).toUpperCase();
  return (
    <p style={{ fontSize: 14 }}>
      derived from props: <strong>{fullName}</strong> ({initials})
    </p>
  );
}

export default function App() {
  const [label, setLabel] = useState("A");
  const [resetToken, setResetToken] = useState(0);

  return (
    <div style={{ padding: 24, fontFamily: "system-ui", lineHeight: 1.6, maxWidth: 460 }}>
      {/* Changing resetToken changes the key, which remounts Counter and
          therefore resets its state. No effect, no manual clearing. */}
      <Counter key={resetToken} label={label} />

      <p>
        <button onClick={() => setLabel((l) => (l === "A" ? "B" : "A"))}>
          change the label prop
        </button>{" "}
        <button onClick={() => setResetToken((t) => t + 1)}>
          change the key (resets state)
        </button>
      </p>

      <Greeting firstName="Ada" lastName="Lovelace" />

      <p style={{ color: "#666", fontSize: 13 }}>
        Click +1 a few times, then change the label β€” the count survives, because
        props and state are independent. Change the key and it drops to 0,
        because React unmounted that instance and mounted a fresh one.
      </p>
    </div>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 22 of 119 in React.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All React.js questions
Up nextEasy

What are React Components?

Continue