javascript - 无法调用 setState(或 forceUpdate)

标签 javascript react-native

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

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/

相关文章:

php - Javascript 无法在 Internet Explorer 上运行

javascript - 如何使用 HTML5 来验证日期范围?

javascript - 我在react-native中获取了带有fetch的JSON,如何将结果发送到其他组件

reactjs - 功能 react native 组件中的 onPress 不起作用

javascript - 如何添加新 key :value pair to an already existing array state variable react native

javascript - 确定 JavaScript 中图像的创建日期?

javascript - 我如何连接我的功能检查 Key Pressed 以更改我在 javascript 中的文本的颜色?

javascript - 具有不透明度和类似模态行为的向导

react-native - react native : Command `run-ios` unrecognized

javascript - React Native,Android 生命周期和导航