Skip to content

Commit

Permalink
fix: element remove leak (#55)
Browse files Browse the repository at this point in the history
* fix:element remove leak

* 当元素不可见时 在removekey获取到的state保证是最新的

* Clear code

---------

Co-authored-by: zhourengui <Nr@*y3FsBGg2>
Co-authored-by: afc163 <afc163@gmail.com>
  • Loading branch information
3 people authored Sep 19, 2024
1 parent 4da2b5f commit 072d50d
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/CSSMotionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,31 @@ export function genCSSMotionList(

// ZombieJ: Return the count of rest keys. It's safe to refactor if need more info.
removeKey = (removeKey: React.Key) => {
const { keyEntities } = this.state;
const nextKeyEntities = keyEntities.map(entity => {
if (entity.key !== removeKey) return entity;
return {
...entity,
status: STATUS_REMOVED,
};
});

this.setState({
keyEntities: nextKeyEntities,
});

return nextKeyEntities.filter(({ status }) => status !== STATUS_REMOVED)
.length;
this.setState(
prevState => {
const nextKeyEntities = prevState.keyEntities.map(entity => {
if (entity.key !== removeKey) return entity;
return {
...entity,
status: STATUS_REMOVED,
};
});

return {
keyEntities: nextKeyEntities,
};
},
() => {
const { keyEntities } = this.state;
const restKeysCount = keyEntities.filter(
({ status }) => status !== STATUS_REMOVED,
).length;

if (restKeysCount === 0 && this.props.onAllRemoved) {
this.props.onAllRemoved();
}
},
);
};

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

if (!changedVisible) {
const restKeysCount = this.removeKey(eventProps.key);

if (restKeysCount === 0 && onAllRemoved) {
onAllRemoved();
}
this.removeKey(eventProps.key);
}
}}
>
Expand Down

0 comments on commit 072d50d

Please sign in to comment.