【发布时间】:2021-01-30 16:38:10
【问题描述】:
我有一个使用 react-spring 库循环动画的组件。 当通过“isVisible”卸载组件时,react 抱怨内存泄漏:
警告:无法对未安装的组件执行 React 状态更新。这是一个空操作,但它表明您的应用程序中存在内存泄漏。要解决此问题,请在 useEffect 清理函数中取消所有订阅和异步任务。
以下是导致问题的代码的 sn-p。
有没有更好的方法/正确的方法来循环 react-spring 的动画而不会出现内存泄漏问题?
import React from 'react';
import { useTransition } from 'react-spring';
export let component = (props) => {
const { isVisible } = props;
const transitions = useTransition(isVisible, null, {
config: { duration: 5000 },
from: { opacity: 1, translateX: 500 },
enter: item => async (next, cancel) => {
while (isVisible) {
await next({ config: { duration: 5000 }, translateX: -1 * 500 });
await next({ config: { duration: 50 }, opacity: 0 });
await next({ config: { duration: 50 }, translateX: 500 });
await next({ config: { duration: 50 }, opacity: 1 });
};
},
});
return transitions.map(({ item, key, props}) => (
<AnimatedView
key={key}
style={[
{
transform: [{ translateX: props.translateX }],
opacity: props.opacity
}
]}
>
{/.../}
</AnimatedView>
))
}
【问题讨论】:
标签: reactjs react-native expo react-spring