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
| Props | State | |
|---|---|---|
| Owned by | the parent | the component itself |
| Changeable by the component? | No, read-only | Yes, through the setter (setX) |
| Purpose | configure a component and pass data down | data 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?