Props 是别人给的,State 是自己记的。
function AirConditioner() {
const [isOn, setIsOn] = useState(false);
const [temp, setTemp] = useState(26);
const [mode, setMode] = useState('cool');
return (
<div>
<p>{isOn ? '运行中' : '已关机'}</p>
<p>模式: {mode} | 温度: {temp}°C</p>
</div>
);
}State vs Props 对比:
| Props | State | |
|---|---|---|
| 谁控制 | 父组件 | 自己 |
| 能改吗 | ❌ 只读 | ✅ 用 set 函数改 |
| 类比 | 遥控器发来的指令 | 空调自己的记忆面板 |