初级⏱️ 15 分钟
useState 入门
学习如何在 React 函数组件中使用 state 来管理组件内部状态
完成进度
0%
0 / 5 步
已用时间
0秒
预计剩余
17分钟
学习进度步骤 1 / 5
步骤
第 1 / 5 步
什么是 State?
State 是组件内部的数据,当 state 改变时,React 会重新渲染组件。
在函数组件中,我们使用 `useState` Hook 来添加 state。
```jsx
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
// count 是当前的 state 值
// setCount 是更新 state 的函数
// 0 是 state 的初始值
}
```
⏱️ 预计完成时间: 3 分钟