← React Beginner Questions - Set 2 Q17

What is the difference between props and state?

React Beginner Questions - Set 2

One example makes the boundary visible: the parent owns the state, the child only receives it as props and reports back through a callback.

Example

import { useState } from 'react';

// Child: no state of its own, props in, event out
function Temperature({ celsius, onWarm }) {
  return (
    <div>
      <p>{celsius}°C</p>
      <button onClick={onWarm}>Warm up</button>
    </div>
  );
}

// Parent: owns the value and the rule that changes it
export default function Thermostat() {
  const [celsius, setCelsius] = useState(20);

  return <Temperature celsius={celsius} onWarm={() => setCelsius(c => c + 1)} />;
}

What to notice

← Back to the question list