File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change
1
+ import { updateAttributes } from './react-dom' ;
2
+
1
3
// 从根节点开始 commit
2
4
export function commitRoot ( rootFiber ) {
3
5
commitWork ( rootFiber . child ) ;
@@ -21,6 +23,11 @@ function commitWork(fiber) {
21
23
// targetPositionDom 不存在,插入到最后
22
24
parentDom . appendChild ( fiber . stateNode ) ;
23
25
}
26
+ } else if ( fiber . flag === 'Update' ) {
27
+ const { children, ...newAttributes } = fiber . element . props ;
28
+ const oldAttributes = Object . assign ( { } , fiber . alternate . element . props ) ;
29
+ delete oldAttributes . children ;
30
+ updateAttributes ( fiber . stateNode , newAttributes , oldAttributes ) ;
24
31
}
25
32
26
33
commitWork ( fiber . sibling ) ;
Original file line number Diff line number Diff line change @@ -47,7 +47,33 @@ export function renderDom(element) {
47
47
}
48
48
49
49
// 更新 dom 属性
50
- function updateAttributes ( dom , attributes ) {
50
+ export function updateAttributes ( dom , attributes , oldAttributes ) {
51
+ if ( oldAttributes ) {
52
+ // 有旧属性,移除旧属性
53
+ Object . keys ( oldAttributes ) . forEach ( ( key ) => {
54
+ if ( key . startsWith ( 'on' ) ) {
55
+ // 移除旧事件
56
+ const eventName = key . slice ( 2 ) . toLowerCase ( ) ;
57
+ dom . removeEventListener ( eventName , oldAttributes [ key ] ) ;
58
+ } else if ( key === 'className' ) {
59
+ // className 的处理
60
+ const classes = oldAttributes [ key ] . split ( ' ' ) ;
61
+ classes . forEach ( ( classKey ) => {
62
+ dom . classList . remove ( classKey ) ;
63
+ } ) ;
64
+ } else if ( key === 'style' ) {
65
+ // style处理
66
+ const style = oldAttributes [ key ] ;
67
+ Object . keys ( style ) . forEach ( ( styleName ) => {
68
+ dom . style [ styleName ] = 'initial' ;
69
+ } ) ;
70
+ } else {
71
+ // 其他属性的处理
72
+ dom [ key ] = '' ;
73
+ }
74
+ } ) ;
75
+ }
76
+
51
77
Object . keys ( attributes ) . forEach ( ( key ) => {
52
78
if ( key . startsWith ( 'on' ) ) {
53
79
// 事件的处理
You can’t perform that action at this time.
0 commit comments