React Hooks 与 Fiber:原理与应用

一、React Hooks 的底层实现

1.1 核心问题

函数组件每次渲染都会重新执行,本身不保留状态。Hooks 如何「记住」上一次的值?

1.2 本质:链表 + 调用顺序

React 为每个组件实例维护一个 Fiber 节点,Fiber 上有 memoizedState,是一条链表

1
2
3
4
useState(0)   → Node1 { value: 0 }
useState('') → Node2 { value: '' }
useEffect(fn) → Node3 { effect: fn }
useMemo(calc) → Node4 { value: result }

Hooks 按调用顺序依次读取/更新链表上的节点,不做「按名字查找」。

1.2.1 示例:状态如何挂在 Fiber 链表上

组件与 Fiber 一一对应:每个组件实例有一个 Fiber 节点,Fiber 的 memoizedState 指向 Hooks 链表。

Counter 组件为例:

1
2
3
4
5
6
function Counter() {
const [count, setCount] = useState(0); // Hook 1
const [name, setName] = useState(''); // Hook 2
const [flag, setFlag] = useState(false); // Hook 3
return <div>{count}</div>;
}

首次渲染时,React 在 Fiber 上建立如下链表结构:

1
2
3
4
5
6
7
8
9
Fiber (Counter 组件)
└── memoizedState (链表头)

▼ Node1: { memoizedState: 0 } ← useState(0)
│ next ──────────────────────────────────┐
▼ Node2: { memoizedState: '' } ← useState('')
│ next ──────────────────────────────────┐
▼ Node3: { memoizedState: false } ← useState(false)
next: null

简化版实现(核心逻辑):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const fiber = { type: Counter, memoizedState: null };
let currentHook = null;

function useState(initialValue) {
if (!currentHook) {
const hook = { memoizedState: initialValue, next: null };
// 挂到 fiber.memoizedState 链表...
currentHook = hook;
}
const hook = currentHook;
currentHook = hook.next;

const setState = (newValue) => {
hook.memoizedState = newValue; // 直接改链表节点
scheduleReRender();
};
return [hook.memoizedState, setState];
}

// 每次渲染前重置为链表头
function renderComponent(Component) {
currentHook = fiber.memoizedState;
return Component();
}

两次渲染的流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
首次渲染:
useState(0) → 创建 Node1 { memoizedState: 0 } 返回 [0, setCount]
useState('') → 创建 Node2 { memoizedState: '' } 返回 ['', setName]
useState(false)→ 创建 Node3 { memoizedState: false } 返回 [false, setFlag]

用户点击,setCount(1):
Node1.memoizedState = 1
scheduleReRender()

第二次渲染:
currentHook = fiber.memoizedState (Node1)
useState(0) → 读 Node1,返回 [1, setCount] ✓
useState('') → 读 Node2,返回 ['', setName] ✓
useState(false)→ 读 Node3,返回 [false, setFlag] ✓
调用顺序 Hook 对应链表节点 存的值
1 useState(0) Node1 0 → 1
2 useState(‘’) Node2 ‘’
3 useState(false) Node3 false

结论:状态不在组件函数里,而在该组件对应 Fiber 的 memoizedState 链表上,每个 Hook 按顺序对应链表中的一个节点。

1.3 为什么顺序不能变?

  • 第 1 次调用 Hook → 用链表第 1 个节点
  • 第 2 次调用 Hook → 用链表第 2 个节点
  • 顺序变了,就错位了

因此 Rules of Hooks 要求:顶层调用、不在循环/条件中。

1.4 各 Hook 的简化实现

useState:

1
2
3
4
5
6
7
8
9
10
11
12
13
let hooks = [];
let currentHookIndex = 0;

function useState(initialValue) {
const index = currentHookIndex;
const state = hooks[index] !== undefined ? hooks[index] : initialValue;
const setState = (newValue) => {
hooks[index] = newValue;
scheduleReRender();
};
currentHookIndex++;
return [state, setState];
}

useEffect: 将 callback 和 deps 存入链表,在 commit 阶段执行;依赖变化时重新执行。

useCallback / useMemo: 缓存上一次的值/函数,依赖数组变化才重新计算/创建。

1.4.1 useEffect 的三种情况

写法 何时执行 常见用途
useEffect(fn) 每次渲染后 很少用
useEffect(fn, []) 仅首次挂载后 订阅、初始化、只跑一次
useEffect(fn, [a, b]) a 或 b 变化时 依赖变化时执行副作用

实现思路:React 在链表节点上存「上次的 callback」和「上次的 deps」,每次渲染时比较 deps(用 Object.is):

  • 无 deps → 每次都执行
  • 空数组 [] → 首次执行(prevDeps 为空)
  • 有 deps → 逐项比较,任一变则执行
1
2
3
4
5
// 简化实现
let shouldRun = false;
if (!deps) shouldRun = true; // 无 deps:每次都执行
else if (!prevDeps) shouldRun = true; // 首次挂载
else shouldRun = deps.some((d, i) => !Object.is(d, prevDeps[i]));

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
// 1. 无 deps:每次渲染后执行
useEffect(() => { console.log('每次渲染都执行'); });

// 2. 空 deps:仅首次
useEffect(() => {
console.log('只执行一次');
return () => console.log('卸载时执行');
}, []);

// 3. 有 deps:依赖变化时执行
useEffect(() => {
console.log('count 或 name 变化时执行');
}, [count, name]);
情况 依赖比较 是否执行
无 deps 不比较 每次
[] 首次 prevDeps 为空 只执行一次
[a,b] 用 Object.is 逐项比较 a 或 b 变化时才执行

1.5 小结

概念 本质
状态存储 存在 Fiber 上的链表,不在函数内部
Hook 识别 调用顺序对应到链表节点
Rules of Hooks 保证顺序和数量恒定,才能一一对应

1.6 Hooks 的实现本质

Hooks 的实现本质可归纳为三点:

  1. 外部存储:状态不存于组件函数内部,而是存在 Fiber 的 memoizedState 链表上。组件每次渲染都会重新执行,但链表在组件实例的生命周期内持续存在。

  2. 顺序索引:每次调用 Hook 时,React 通过一个「当前索引」决定读写哪个链表节点。索引随调用递增,因此必须保证每次渲染的调用顺序、数量完全一致。

  3. 调度更新setState 等更新函数会修改链表上的值,并触发 React 的调度器安排一次重渲染。下次渲染时,组件函数重新执行,Hooks 按相同顺序从链表中读出最新值。

三者结合:外部存储解决「函数无状态」问题,顺序索引解决「多个 Hook 如何区分」问题,调度更新解决「变化如何驱动重渲染」问题。

1.7 闭包机制与 Hooks 的关系

Hooks 与闭包紧密相关:

1. setState 依赖闭包捕获索引

1
2
3
4
const setState = (newValue) => {
hooks[index] = newValue; // index 来自闭包
scheduleReRender();
};

setState 在首次渲染时创建,通过闭包捕获了当时的 index。即使用户在 3 秒后点击按钮调用 setState,它仍然能正确写入对应节点,因为闭包保留了 index

2. stale closure(陈旧闭包)问题

事件回调、useEffect 中的函数会闭包捕获当次渲染时的 state。若在回调中直接使用 state,可能拿到旧值:

1
2
3
4
5
6
7
8
9
function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => {
setCount(count + 1); // count 被闭包捕获,一直是 0
}, 1000);
return () => clearInterval(id);
}, []);
}

3. 正确做法:函数式更新

1
setCount(prev => prev + 1);  // 传入函数,React 注入最新 state

React 会传入最新的 state,避免闭包捕获 stale 值。

4. useCallback / useMemo 与闭包

useCallback(fn, deps) 缓存的是函数本身;若 deps 不变,返回的是同一个函数引用,其闭包捕获的也是旧依赖。useMemo 同理。因此 deps 必须完整列出闭包中用到的所有变量。

1.8 详细分析:一次更新的完整流程

useState 为例,从点击按钮到界面更新:

1
2
3
4
5
6
7
1. 用户点击 → 触发 onClick → 调用 setState(newValue)
2. setState 通过闭包中的 index 找到链表节点,写入新值
3. 调用 scheduleReRender(),React 将本次更新加入调度队列
4. 调度器在合适时机执行 render 阶段
5. 组件函数重新执行,useState(initialValue) 再次被调用
6. currentHookIndex 从 0 开始,按顺序遍历:hooks[index] 已有新值,返回 [newValue, setState]
7. 组件用新 state 生成新 JSX,进入 commit 阶段,DOM 更新

闭包保证了:异步回调中的 setState,在任意时刻被调用时,仍能通过 index 找到正确的链表节点。 链表保证了:多次渲染之间,状态得以保留。 两者缺一不可。


二、Fiber 的核心原理

2.1 一句话

Fiber 把「一次性递归完整个树」的同步更新,拆成「按节点逐步执行、可中断」的增量更新,让渲染不卡顿主线程。

2.2 为什么需要 Fiber?

旧版 (Stack Reconciler) Fiber 之后
从根递归整棵树,一气做完 拆成一个个小单元
无法暂停 可随时暂停,让出主线程
大树会长时间占用主线程 支持时间分片、优先级调度

2.3 Fiber 是什么?

Fiber = 对应一个 React 元素的「工作单元」,携带该节点的类型、props、子节点引用等。

1
2
3
4
5
6
7
8
9
Fiber {
type, key, props
child: 第一个子 Fiber
sibling: 下一个兄弟 Fiber
return: 父 Fiber(用于回溯)
alternate: 另一棵树的对应 Fiber(双缓冲)
flags: 增/删/改等标记
lane: 优先级
}

2.4 链表式遍历(而非递归)

树被转成链表结构,遍历时可随时停下、下次从断点继续:

1
2
3
4
5
6
7
        App
/ \
Header Main
/ \ |
Nav Logo Content

遍历顺序:App → Header → Nav → Logo → Main → Content ...

2.5 双缓冲 (alternate)

  • current:当前屏幕上的树
  • workInProgress:正在构建的新树

更新时在 workInProgress 上增量构建,commit 阶段一次性切换,避免闪烁。

2.6 总结

问题 回答
Fiber 是什么? 一个工作单元,带 child/sibling/return 的链表节点
为什么用? 实现可中断、可恢复、可调度
如何可中断? 链表遍历 + 时间分片
双缓冲? 两棵树交替,保证更新可复用且一次性提交

三、Fiber 作为一种设计思想

3.1 Fiber 可抽象为通用思想

思想 含义
可中断的增量工作 大任务拆成小单元,做一点可停
时间分片 固定时间片内工作,到点让出主线程
优先级调度 高优任务(如用户输入)插队
链表式遍历 用 child/sibling/return 实现可暂停、可恢复

这些思想可迁移到 UI 渲染、动画、大数据处理等场景。

3.2 Flutter 能否引入?

可以。Flutter 已具备类似机制:

Flutter 机制 对应思想
SchedulerBinding 分阶段调度(microtask、frame、idle)
优先级队列 高优任务优先执行
Isolate 重计算放到后台,减轻 UI 线程压力

若要在 layout 等环节做「增量可中断」,可借鉴 Fiber 的拆解与调度方式。

3.3 通用模式

1
2
3
4
5
1. 把大任务拆成小工作单元
2. 每个单元可单独执行、可暂停
3. 用链表/树记录进度,便于恢复
4. 调度器按优先级和时间片决定执行哪些
5. 高优任务可插队

四、整体逻辑关系

1
2
3
4
5
6
7
8
9
Hooks 底层
→ 状态存在 Fiber 的链表上,靠调用顺序对应
→ 闭包捕获索引,保证 setState 等更新函数能正确写入;需注意 stale closure

Fiber 核心
→ 把递归更新改为可中断的链表遍历 + 时间分片

Fiber 思想
→ 可中断、可调度、增量工作,具有普适性,可迁移到其他框架(如 Flutter)与场景

React Hooks

从基本概念到源码实现,全面解析 React Hooks


一、基本概念

1.1 什么是 Hooks?

Hooks 是 React 16.8 引入的新特性,它允许你在不编写 class 的情况下使用 state 以及其他的 React 特性。

简单来说,Hooks 就是让你在函数组件中”钩入” React 特性的函数

1.2 为什么需要 Hooks?

在 Hooks 出现之前,React 存在以下问题:

痛点 描述
状态逻辑难复用 使用 render propsHOC 会导致组件嵌套地狱
复杂组件难理解 生命周期中充斥大量无关逻辑,难以拆分
Class 的学习成本 需要理解 this、绑定事件、生命周期等概念
逻辑分散 相关逻辑散落在 componentDidMountcomponentDidUpdatecomponentWillUnmount

1.3 核心 Hooks 一览

Hook 作用
useState 在函数组件中添加 state
useEffect 执行副作用(订阅、请求、DOM 操作等)
useContext 读取 Context 值
useReducer useState 的替代方案,适合复杂 state 逻辑
useCallback 缓存回调函数
useMemo 缓存计算结果
useRef 引用 DOM 或保存可变值
useLayoutEffect 同步执行副作用,在 DOM 更新后、浏览器绘制前

二、常用 Hooks 详解与示例

2.1 useState

1
2
3
4
5
6
7
8
9
10
11
12
13
function Counter() {
const [count, setCount] = useState(0);
const [user, setUser] = useState({ name: '', age: 0 });

return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>+1</button>
{/* 函数式更新,适合依赖前一个 state */}
<button onClick={() => setCount(prev => prev + 1)}>+1 (函数式)</button>
</div>
);
}

关键点:

  • useState 接收初始值,返回 [当前值, 更新函数]
  • 初始值可以是函数(惰性初始化),用于避免每次渲染都执行 expensive 计算
1
const [state, setState] = useState(() => computeExpensiveInitialState());

2.2 useEffect

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function DataFetcher({ id }) {
const [data, setData] = useState(null);

useEffect(() => {
const controller = new AbortController();
fetch(`/api/users/${id}`, { signal: controller.signal })
.then(res => res.json())
.then(setData)
.catch(console.error);

// 清理函数:组件卸载或 effect 重新执行前调用
return () => controller.abort();
}, [id]); // 依赖数组:id 变化时重新执行

return <div>{data ? JSON.stringify(data) : 'Loading...'}</div>;
}

依赖数组规则:

  • 不传:每次渲染后都执行
  • []:仅挂载时执行一次
  • [a, b]:a 或 b 变化时执行

2.3 useRef

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function TextInput() {
const inputRef = useRef(null);

const focusInput = () => {
inputRef.current?.focus();
};

return (
<>
<input ref={inputRef} />
<button onClick={focusInput}>聚焦</button>
</>
);
}

// 保存可变值,不触发重渲染
function Timer() {
const countRef = useRef(0);
countRef.current++; // 修改不会触发渲染
return <div>Render count: {countRef.current}</div>;
}

2.4 useCallback 与 useMemo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function Parent() {
const [count, setCount] = useState(0);
const [text, setText] = useState('');

// 缓存函数,避免子组件因引用变化而重渲染
const handleClick = useCallback(() => {
setCount(c => c + 1);
}, []);

// 缓存计算结果
const expensiveValue = useMemo(() => {
return computeExpensiveValue(count);
}, [count]);

return <MemoChild onClick={handleClick} value={expensiveValue} />;
}

三、Hooks 原理浅析

3.1 核心思想:链表存储

React 在内部用链表来存储 Hooks。每个函数组件对应一个 Fiber 节点,每个 Hook 是链表上的一个节点。

1
2
3
4
5
Fiber 节点
└── memoizedState (第一个 Hook)
├── next -> (第二个 Hook)
│ ├── next -> (第三个 Hook)
│ │ └── ...

为什么是链表?

  • 函数组件没有 this,无法像 Class 那样用实例存储
  • 链表可以按调用顺序保存多个 Hook
  • 每次渲染时按顺序遍历链表,对应到正确的 state

3.2 两个重要的链表

React 内部维护两套 Hook 链表:

  1. current:当前屏幕上显示的 UI 对应的 Hooks
  2. workInProgress:正在构建的新的 Hooks

渲染流程大致为:

1
2
3
mount -> workInProgress 构建新链表
update -> 从 current 复制到 workInProgress,按顺序更新
commit -> workInProgress 替换 current

3.3 为什么 Hooks 必须在顶层调用?

1
2
3
4
5
6
7
// ❌ 错误:条件调用
function Bad() {
if (condition) {
const [a, setA] = useState(0); // 破坏调用顺序!
}
const [b, setB] = useState(0);
}

React 依赖 Hooks 的调用顺序 来将 state 与正确的 Hook 关联。如果顺序变化,会错位导致 bug。


四、源码解读

4.1 简化的 Hook 数据结构

1
2
3
4
5
6
7
8
// React 内部简化版
type Hook = {
memoizedState: any, // 当前 state 值
baseState: any, // 基础 state
baseQueue: Update | null,
queue: UpdateQueue, // 更新队列
next: Hook | null, // 下一个 Hook(链表)
};

4.2 useState 实现思路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 极简版 useState 实现(理解原理用)
let hooks = [];
let currentHook = 0;

function useState(initialState) {
const index = currentHook;
hooks[index] = hooks[index] ?? (typeof initialState === 'function' ? initialState() : initialState);

function setState(newState) {
hooks[index] = typeof newState === 'function' ? newState(hooks[index]) : newState;
scheduleReRender();
}

currentHook++;
return [hooks[index], setState];
}

function useMyComponent() {
currentHook = 0;
const [count, setCount] = useState(0);
const [name, setName] = useState(''); // 按顺序压入链表
// ...
}

4.3 useEffect 实现思路

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function useEffect(callback, deps) {
const index = currentHook;
const prevDeps = hooks[index];

const hasChanged = !prevDeps || deps?.some((d, i) => d !== prevDeps[i]);

if (hasChanged) {
// 先执行上一次的 cleanup
if (typeof prevEffect?.cleanup === 'function') {
prevEffect.cleanup();
}
const cleanup = callback();
hooks[index] = { deps, cleanup };
}
currentHook++;
}

五、自定义 Hook

5.1 封装业务逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// useLocalStorage - 同步 state 到 localStorage
function useLocalStorage(key, initialValue) {
const [storedValue, setStoredValue] = useState(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
} catch (e) {
return initialValue;
}
});

const setValue = useCallback((value) => {
setStoredValue(prev => {
const next = typeof value === 'function' ? value(prev) : value;
window.localStorage.setItem(key, JSON.stringify(next));
return next;
});
}, [key]);

return [storedValue, setValue];
}

// 使用
function Settings() {
const [theme, setTheme] = useLocalStorage('theme', 'light');
return <button onClick={() => setTheme(t => t === 'light' ? 'dark' : 'light')}>{theme}</button>;
}

5.2 useDebounce

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function useDebounce(value, delay) {
const [debouncedValue, setDebouncedValue] = useState(value);

useEffect(() => {
const timer = setTimeout(() => setDebouncedValue(value), delay);
return () => clearTimeout(timer);
}, [value, delay]);

return debouncedValue;
}

// 搜索防抖
function SearchInput() {
const [search, setSearch] = useState('');
const debouncedSearch = useDebounce(search, 300);

useEffect(() => {
if (!debouncedSearch) return;
fetchResults(debouncedSearch);
}, [debouncedSearch]);
// ...
}

5.3 useFetch(数据请求)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function useFetch(url) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

useEffect(() => {
setLoading(true);
const controller = new AbortController();

fetch(url, { signal: controller.signal })
.then(res => res.json())
.then(setData)
.catch(setError)
.finally(() => setLoading(false));

return () => controller.abort();
}, [url]);

return { data, loading, error };
}

六、实际项目应用案例

6.1 场景一:表单管理(复杂表单)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function useForm(initialValues) {
const [values, setValues] = useState(initialValues);
const [errors, setErrors] = useState({});

const handleChange = useCallback((name) => (e) => {
setValues(prev => ({ ...prev, [name]: e.target.value }));
}, []);

const validate = useCallback(() => {
const newErrors = {};
if (!values.email) newErrors.email = '必填';
if (!values.password) newErrors.password = '必填';
setErrors(newErrors);
return Object.keys(newErrors).length === 0;
}, [values]);

const reset = useCallback(() => {
setValues(initialValues);
setErrors({});
}, []);

return { values, errors, handleChange, validate, reset };
}

// 登录表单
function LoginForm() {
const { values, errors, handleChange, validate } = useForm({ email: '', password: '' });

const onSubmit = (e) => {
e.preventDefault();
if (!validate()) return;
login(values);
};

return (
<form onSubmit={onSubmit}>
<input value={values.email} onChange={handleChange('email')} />
{errors.email && <span>{errors.email}</span>}
<input type="password" value={values.password} onChange={handleChange('password')} />
{errors.password && <span>{errors.password}</span>}
<button type="submit">登录</button>
</form>
);
}

6.2 场景二:无限滚动列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function useInfiniteScroll(callback, hasMore, loading) {
const observerRef = useRef();
const lastElementRef = useCallback(node => {
if (loading) return;
if (observerRef.current) observerRef.current.disconnect();
observerRef.current = new IntersectionObserver(entries => {
if (entries[0].isIntersecting && hasMore) {
callback();
}
});
if (node) observerRef.current.observe(node);
}, [loading, hasMore, callback]);

return lastElementRef;
}

function InfiniteList() {
const [items, setItems] = useState([]);
const [page, setPage] = useState(1);
const [hasMore, setHasMore] = useState(true);
const [loading, setLoading] = useState(false);

const loadMore = useCallback(async () => {
setLoading(true);
const res = await fetchItems(page);
setItems(prev => [...prev, ...res.data]);
setHasMore(res.hasMore);
setPage(p => p + 1);
setLoading(false);
}, [page]);

const lastRef = useInfiniteScroll(loadMore, hasMore, loading);

return (
<ul>
{items.map(item => <li key={item.id}>{item.name}</li>)}
<li ref={lastRef} style={{ height: 1 }} />
</ul>
);
}

6.3 场景三:权限与路由守卫

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function useAuth() {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);

useEffect(() => {
authApi.getCurrentUser()
.then(setUser)
.finally(() => setLoading(false));
}, []);

const hasPermission = useCallback((permission) => {
return user?.permissions?.includes(permission);
}, [user]);

return { user, loading, hasPermission };
}

function ProtectedRoute({ permission, children }) {
const { user, loading, hasPermission } = useAuth();

if (loading) return <Spin />;
if (!user) return <Navigate to="/login" />;
if (permission && !hasPermission(permission)) return <Forbidden />;

return children;
}

6.4 场景四:性能优化(虚拟列表 + useMemo)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function VirtualList({ items, itemHeight }) {
const [scrollTop, setScrollTop] = useState(0);
const containerRef = useRef();

const visibleRange = useMemo(() => {
const start = Math.floor(scrollTop / itemHeight);
const end = start + Math.ceil(containerRef.current?.clientHeight / itemHeight) + 2;
return [Math.max(0, start), Math.min(items.length, end)];
}, [scrollTop, itemHeight, items.length]);

const visibleItems = useMemo(() => {
const [start, end] = visibleRange;
return items.slice(start, end).map((item, i) => ({
...item,
index: start + i,
}));
}, [items, visibleRange]);

return (
<div
ref={containerRef}
onScroll={e => setScrollTop(e.target.scrollTop)}
style={{ height: '100%', overflow: 'auto' }}
>
<div style={{ height: items.length * itemHeight, position: 'relative' }}>
{visibleItems.map(item => (
<div key={item.id} style={{ position: 'absolute', top: item.index * itemHeight }}>
{item.content}
</div>
))}
</div>
</div>
);
}

七、常见陷阱与最佳实践

7.1 闭包陷阱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// ❌ 错误:setTimeout 中拿到的是旧 count
function Bad() {
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => {
setCount(count + 1); // count 永远是 0
}, 1000);
return () => clearInterval(id);
}, []); // 缺少 count 依赖
}

// ✅ 正确:使用函数式更新
function Good() {
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => {
setCount(c => c + 1);
}, 1000);
return () => clearInterval(id);
}, []);
}

7.2 依赖数组的完整性

  • 建议使用 eslint-plugin-react-hooksexhaustive-deps 规则
  • 不要随意省略依赖,可能导致 stale closure

7.3 何时用 useCallback / useMemo

  • 需要传递回调给 memo 子组件时用 useCallback
  • 计算成本高且依赖稳定时用 useMemo
  • 不要过度使用,有时重新创建的开销小于记忆化

八、总结

主题 要点
概念 Hooks 让函数组件拥有 state 和副作用能力
原理 链表存储,依赖调用顺序,current/workInProgress 双缓冲
源码 极简实现可帮助理解,真实实现见 React 仓库
自定义 抽取可复用逻辑,命名以 use 开头
实践 避免闭包陷阱,合理使用依赖数组,按需优化

React Hooks 是现代 React 开发的核心,掌握其原理与最佳实践,能显著提升代码质量与开发效率。


参考资料