在 React 底层中,一个 Fiber 节点代表一个组件或 DOM 元素,它包含了组件的状态、子节点、兄弟节点以及其他与组件相关的信息。一个 Fiber 节点长这样:
{
tag: 0, // 组件类型,0 表示函数组件,1 表示类组件,2 表示原生 DOM 元素
key: null, // 组件的 key 值
elementType: type, // 组件的类型
type: FunctionComponent, // 组件的函数或类
stateNode: null, // 组件实例或 DOM 元素
return: null, // 父节点
child: null, // 第一个子节点
sibling: null, // 兄弟节点
index: 0, // 在兄弟节点中的索引
ref: null, // 组件的 ref 属性
pendingProps: nextProps, // 组件的 props
memoizedProps: null, // 上一次渲染时的 props
memoizedState: null, // 上一次渲染时的 state
updateQueue: null, // 更新队列
mode: mode, // 渲染模式
effectTag: NoEffect, // 副作用标记
nextEffect: null, // 下一个副作用
firstEffect: null, // 第一个副作用
lastEffect: null, // 最后一个副作用
}
举个例子,假设有一个简单的函数组件ÿ