我已经构建了一个学习管理系统应用程序in react native。我使用AsyncStorage
进行简单的状态管理而根本没有使用redux。我现在面临的问题是如果我要通过执行来连续使用app不同的行动,然后应用程序变得非常慢。我认为这是内存泄漏,因为当我从后台杀死应用程序并再次打开它时它没有任何延迟工作。所以我不知道如何避免这种内存泄漏。我已经尝试了很多解决方
console.log
ComponentDidMount
而不是ComponentWillMount
。但是我不知道如何从堆内存中删除数据。这些数据是否存储在每个导航的heap
on中?所以这会使应用程序的性能非常慢。我不知道我是对的。请问我的概念是否有任何错误。现在没有时间将状态管理改为redux
。任何人请帮我找一个解决方案,这将是一个很大的帮助。谢谢!
我有同样的问题,帮助的方法很少:
使用transform-remove-console:
https://www.npmjs.com/package/babel-plugin-transform-remove-console
将此添加到您的babel生产插件并安装它。它将隐藏生产中应用程序中的所有控制台日志。
添加挂载状态
具体来说,在卸载组件中调用setState()意味着您的应用程序在卸载组件后仍然保持对组件的引用 - 这通常表示内存泄漏!
https://reactjs.org/blog/2015/12/16/ismounted-antipattern.html
import React, { Component } from 'react';
class App extends Component {
_isMounted = false;
componentDidMount() {
this._isMounted = true;
// ... do your stuff here
}
componentWillUnmount() {
// tells the component that component is now unmounted
this._isMounted = false;
}
getUsersFromApi(){
if(this._isMounted){
// ... tasks
}
}
}
export default App;
我也有同样的问题,因为在未安装的组件上调用setState
,
所以,我通常有这个模板用于任何具有状态的基于类的组件:
我忘记了setState()
,并使用setComponentState
宣布:
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
// other fields...
isUnmounted: false,
};
}
componentWillUnmount() {
this.setState({ isUnmounted: true });
}
setComponentState = (values) => {
if (!this.state.isUnmounted) this.setState(values);
};
}