File tree Expand file tree Collapse file tree 3 files changed +25
-9
lines changed Expand file tree Collapse file tree 3 files changed +25
-9
lines changed Original file line number Diff line number Diff line change
1
+ // 从根节点开始 commit
2
+ export function commitRoot ( rootFiber ) {
3
+ commitWork ( rootFiber . child ) ;
4
+ }
5
+
6
+ // 递归执行 commit,此过程不中断
7
+ function commitWork ( fiber ) {
8
+ if ( ! fiber ) {
9
+ return ;
10
+ }
11
+ // 深度优先遍历,先遍历 child,后遍历 sibling
12
+ commitWork ( fiber . child ) ;
13
+ let parentDom = fiber . return . stateNode ;
14
+ parentDom . appendChild ( fiber . stateNode ) ;
15
+ commitWork ( fiber . sibling ) ;
16
+ }
Original file line number Diff line number Diff line change 1
1
import { renderDom } from './react-dom' ;
2
+ import { commitRoot } from './commit' ;
2
3
3
4
let nextUnitOfWork = null ;
4
5
let rootFiber = null ;
@@ -21,15 +22,6 @@ function performUnitOfWork(workInProgress) {
21
22
// 若当前 fiber 没有 stateNode,则根据 fiber 挂载的 element 的属性创建
22
23
workInProgress . stateNode = renderDom ( workInProgress . element ) ;
23
24
}
24
- if ( workInProgress . return && workInProgress . stateNode ) {
25
- // 如果 fiber 有父 fiber且有 dom
26
- // 向上寻找能挂载 dom 的节点进行 dom 挂载
27
- let parentFiber = workInProgress . return ;
28
- while ( ! parentFiber . stateNode ) {
29
- parentFiber = parentFiber . return ;
30
- }
31
- parentFiber . stateNode . appendChild ( workInProgress . stateNode ) ;
32
- }
33
25
34
26
let children = workInProgress . element ?. props ?. children ;
35
27
@@ -111,6 +103,11 @@ function workLoop(deadline) {
111
103
performUnitOfWork ( nextUnitOfWork ) ;
112
104
shouldYield = deadline . timeRemaining ( ) < 1 ;
113
105
}
106
+ if ( ! nextUnitOfWork && rootFiber ) {
107
+ // 表示进入 commit 阶段
108
+ commitRoot ( rootFiber ) ;
109
+ rootFiber = null ;
110
+ }
114
111
requestIdleCallback ( workLoop ) ;
115
112
}
116
113
Original file line number Diff line number Diff line change @@ -33,6 +33,9 @@ export function renderDom(element) {
33
33
if ( typeof type === 'string' ) {
34
34
// 常规 dom 节点的渲染
35
35
dom = document . createElement ( type ) ;
36
+ } else if ( typeof type === 'function' ) {
37
+ // React 组件的渲染
38
+ dom = document . createDocumentFragment ( ) ;
36
39
} else {
37
40
// 其他情况暂不考虑
38
41
return null ;
You can’t perform that action at this time.
0 commit comments