TechnicalEasy React

What is the difference between props and state in React?

Asked for Frontend Developer, Full-Stack Developer, Mobile Developer

Why they ask this: The most basic React concept; a weak answer is a red flag for any React role.

Answer

PropsState
Owned bythe parentthe component itself
Changeable by the component?No, read-onlyYes, through the setter (setX)
Purposeconfigure a component and pass data downdata that changes over time
jsx
function Counter({ step }) {          // step is a prop
  const [count, setCount] = useState(0); // count is state
  return <button onClick={() => setCount((c) => c + step)}>{count}</button>;
}

Changing either one re-renders the component. When two siblings need the same data, lift the state up to their common parent and pass it down as props.

Can you explain this in your own words?

Back to question bank