Javascript 反应本机:闪存消息内存泄漏错误
我用的是React Native。当我导航到另一个屏幕而不等待消息消失时,会引发内存泄漏错误 代码 错误Javascript 反应本机:闪存消息内存泄漏错误,javascript,react-native,expo,flash-message,Javascript,React Native,Expo,Flash Message,我用的是React Native。当我导航到另一个屏幕而不等待消息消失时,会引发内存泄漏错误 代码 错误 您不需要等待flash消息消失。您可以在导航到新屏幕之前以编程方式隐藏消息 代码 具有useffect()的解决方案 这个库有清理方法吗?我不这么认为。侦听器和异步操作通常需要清理,我似乎找不到类似的方法。你能分享更多你想要实现的目标吗?@VinaySharma不,这个库没有清理方法。如果我不等待flash消息消失,然后转到另一个页面,我会收到此错误。您是否在全球范围内使用它? showMe
您不需要等待flash消息消失。您可以在导航到新屏幕之前以编程方式隐藏消息 代码
具有useffect()的解决方案
这个库有清理方法吗?我不这么认为。侦听器和异步操作通常需要清理,我似乎找不到类似的方法。你能分享更多你想要实现的目标吗?@VinaySharma不,这个库没有清理方法。如果我不等待flash消息消失,然后转到另一个页面,我会收到此错误。您是否在全球范围内使用它?
showMessage({
message: "Hello World",
duration: 2000,
description: "This is our second message",
type: "success",
});
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in %s.%s, the componentWillUnmount method,
// import hideMessage method from the library
import { hideMessage } from "react-native-flash-message";
// a function which navigates to another screen
foo = () => {
// assuming you are using react navigation
// in a class based component
const { navigation } = this.props;
// call hideMessage before you navigate
hideMessage();
navigation.navigate('ScreenName');
}
import { hideMessage } from "react-native-flash-message";
useEffect(() => {
return () => {
hideMessage()
}
}, [])