Skip to content

Commit 072d50d

Browse files
zhourenguizhourenguiafc163
authored
fix: element remove leak (#55)
* fix:element remove leak * 当元素不可见时 在removekey获取到的state保证是最新的 * Clear code --------- Co-authored-by: zhourengui <Nr@*y3FsBGg2> Co-authored-by: afc163 <afc163@gmail.com>
1 parent 4da2b5f commit 072d50d

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/CSSMotionList.tsx

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,31 @@ export function genCSSMotionList(
110110

111111
// ZombieJ: Return the count of rest keys. It's safe to refactor if need more info.
112112
removeKey = (removeKey: React.Key) => {
113-
const { keyEntities } = this.state;
114-
const nextKeyEntities = keyEntities.map(entity => {
115-
if (entity.key !== removeKey) return entity;
116-
return {
117-
...entity,
118-
status: STATUS_REMOVED,
119-
};
120-
});
121-
122-
this.setState({
123-
keyEntities: nextKeyEntities,
124-
});
125-
126-
return nextKeyEntities.filter(({ status }) => status !== STATUS_REMOVED)
127-
.length;
113+
this.setState(
114+
prevState => {
115+
const nextKeyEntities = prevState.keyEntities.map(entity => {
116+
if (entity.key !== removeKey) return entity;
117+
return {
118+
...entity,
119+
status: STATUS_REMOVED,
120+
};
121+
});
122+
123+
return {
124+
keyEntities: nextKeyEntities,
125+
};
126+
},
127+
() => {
128+
const { keyEntities } = this.state;
129+
const restKeysCount = keyEntities.filter(
130+
({ status }) => status !== STATUS_REMOVED,
131+
).length;
132+
133+
if (restKeysCount === 0 && this.props.onAllRemoved) {
134+
this.props.onAllRemoved();
135+
}
136+
},
137+
);
128138
};
129139

130140
render() {
@@ -160,11 +170,7 @@ export function genCSSMotionList(
160170
onVisibleChanged?.(changedVisible, { key: eventProps.key });
161171

162172
if (!changedVisible) {
163-
const restKeysCount = this.removeKey(eventProps.key);
164-
165-
if (restKeysCount === 0 && onAllRemoved) {
166-
onAllRemoved();
167-
}
173+
this.removeKey(eventProps.key);
168174
}
169175
}}
170176
>

0 commit comments

Comments
 (0)