gpt4 book ai didi

javascript - 无法调用 setState(或 forceUpdate)

转载 作者:行者123 更新时间:2023-11-30 11:06:34 24 4
gpt4 key购买 nike

我在控制台中看到这个错误并尝试了很多解决方案都没有用

ExceptionsManager.js:84 Warning: Can't call setState (or forceUpdate) 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 the componentWillUnmount method. in Start (at SceneView.js:9) in SceneView (at SwitchView.js:12) in SwitchView (at createNavigator.js:57) in Navigator (at createNavigationContainer.js:376) in NavigationContainer (at App.js:95)

我尝试了这些解决方案和许多其他解决方案 Link Link Link Link

App.js

import React from 'react';
import { Platform, StatusBar, Image } from 'react-native';
import { AppLoading, Asset } from 'expo';
import AppPreLoader from './components/AppPreLoader'
import { Block, GalioProvider } from 'galio-framework';
import Screens from './navigation/Screens';
import { Images, materialTheme } from './constants/';
import firebaseConfig from './constants/Firebase';
import * as firebase from 'firebase';
import Notlogged from './navigation/Notlogged';
firebase.initializeApp(firebaseConfig);

// cache app images
const assetImages = [
Images.Onboarding,
];

function cacheImages(images) {
return images.map(image => {
if (typeof image === 'string') {
return Image.prefetch(image);
} else {
return Asset.fromModule(image).downloadAsync();
}
});
}


export default class App extends React.Component {
constructor () {
super();
this.state = {
isLogged: false,
loaded: false,
isReady: false,
}
}

async componentDidMount () {
await firebase.auth().onAuthStateChanged((user) => {
if(user !== null) {
this.setState({
isLogged: true,
loaded: true
});
} else {
this.setState({
isLogged: false,
loaded: true
});
}
})
}

componentWillUnmount(){
}

render() {

if (!this.state.isReady) {
return (
<AppLoading
startAsync={this._loadResourcesAsync}
onFinish={() => this.setState({ isReady: true })}
onError={console.warn}
/>
);
}

const {isLogged, loaded, isReady} = this.state;
if ( ! loaded) {
return (
<AppPreLoader/>
);
}

if(isLogged && isReady) {
return (
<GalioProvider theme={materialTheme}>
<Block flex>
{Platform.OS === 'ios' && <StatusBar barStyle="default" />}
<Screens />
</Block>
</GalioProvider>
);
}else{
return (
<GalioProvider theme={materialTheme}>
<Block flex>
{Platform.OS === 'ios' && <StatusBar barStyle="default" />}
<Notlogged />
</Block>
</GalioProvider>
);
}

}

_loadResourcesAsync = async () => {
return Promise.all([
...cacheImages(assetImages),
]);
};

_handleLoadingError = error => {
console.warn(error);
};

_handleFinishLoading = () => {
this.setState({ isLoadingComplete: true });
};
}

当我签名并且应用程序转到主屏幕时会出现警告,但如果我只是在登录后打开应用程序,则没有警告只有在我登录后才发出警告。

视频显示,如果我已经登录,则不会出现警告,但如果我尝试登录,则会出现警告

https://streamable.com/s/yjt8x/nvgtbh

最佳答案

错误很明显:componentWillUnmount 不应包含 this.setState 或任何修改状态的尝试。在 React documentation 中提到:

You should not call setState() in componentWillUnmount() because the component will never be re-rendered. Once a component instance is unmounted, it will never be mounted again.

编辑:好的,您的组件还有 1 个问题:firebase.auth().onAuthStateChanged() 的处理程序可能会在组件卸载期间或之后运行,这React 检测到尝试更新未安装组件的状态。为了解决这个问题,你可以为你的组件设置一个 bool 值来检查它是否仍然需要执行 setState 或不:

  async componentDidMount () {
this.unmounted = false;
await firebase.auth().onAuthStateChanged((user) => {
if (this.unmounted) {
return false;
}
if(user !== null) {
this.setState({
isLogged: true,
loaded: true
});
} else {
this.setState({
isLogged: false,
loaded: true
});
}
})
}

componentWillUnmount(){
this.unmounted = true;
}

关于javascript - 无法调用 setState(或 forceUpdate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55373055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com