EasyConcept

What is the difference between props and state in React?

Technology:
React
Experience:
Intern,
Junior

Quick answer

Props are read-only inputs passed from a parent component, while state is data a component owns and can change over time, which triggers a re-render when updated.

Detailed explanation

Props make a component reusable: the parent decides the values, and the child must not modify them. State is private memory for a component, created with useState or useReducer. Updating state tells React to re-render that component with the new value.

A good rule is to keep state as low as possible in the tree, and lift it up to the closest common parent only when two components need to share it. Data that can be calculated from props or state should be computed during render and not stored in extra state.

Example

jsx
function Counter({ step }) {            // step is a prop (read-only)
  const [count, setCount] = useState(0);    // count is state (owned here)
  return <button onClick={() => setCount(count + step)}>{count}</button>;
}

<Counter step={5} />

How to explain it in an interview

Props are inputs passed from a parent and are read-only inside the child. State is data a component owns and changes over time with a setter, which triggers a re-render. If two components need the same data, I lift the state to their closest common parent and pass it down as props.

Key points

  • Props flow down, are read-only and come from the parent
  • State is owned by the component and changes cause re-renders
  • Derived values should be calculated, not stored in state

Common mistakes

  • Mutating props or state directly instead of creating a new value.
  • Copying props into state without a reason, which leaves the two out of sync when the prop changes.
  • ReactMedium

    What is prop drilling and how do you avoid it in React?

    Prop drilling is passing props through several layers of components that do not use them just to reach a deeply nested child; you avoid it with composition, the Context API or a state management library.

    Junior 路 Mid-level 路 State Management
  • ReactMedium

    What is the difference between useRef and useState in React?

    Updating state triggers a re-render, while changing a ref's .current value does not; use state for data shown on screen and refs for DOM access or values that must persist without causing renders.

    Mid-level 路 Senior 路 React Hooks
  • ReactEasy

    What is the virtual DOM in React and how does it work?

    The virtual DOM is a lightweight JavaScript copy of the UI that React compares with the previous version after each update, so it only changes the parts of the real DOM that actually differ.

    Intern 路 Junior 路 Performance
  • ReactEasy

    What are React hooks and what are the rules of hooks?

    Hooks are functions such as useState and useEffect that let function components use state, lifecycle behaviour and other React features; they must be called at the top level of a component or custom hook, never inside loops, conditions or nested functions.

    Junior 路 Mid-level 路 React Hooks
  • ReactEasy

    Why do we need keys in React lists?

    Keys give each list item a stable identity so React can match items between renders and correctly add, remove or reorder them without recreating or mixing up their state.

    Junior 路 Mid-level 路 Performance