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
keychange 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.