Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 a useEffect cleanup function #28

Open
ilibilibom opened this issue Dec 22, 2019 · 1 comment

Comments

@ilibilibom
Copy link

Hi
I'm getting this error after user logs in:

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 a useEffect cleanup function
in Facebook (at Hero.tsx:27)
    in RCTView (at Hero.tsx:26)
    in RCTView (at ImageBackground.js:60)
    in ImageBackground (at Hero.tsx:15)
    in RCTView (at Hero.tsx:14)
    in Hero (at SignIn.tsx:15)

I'm can't understand were is this error coming from:
this is Hero:

import React from 'react';
import {Image, SafeAreaView, StyleSheet, View, ViewStyle, ImageBackground, Text} from 'react-native';
import Facebook from '../providers/Facebook';
import Google from '../providers/Google';

interface Props {
  children?: React.ReactNode | React.ReactNode[];
  style?: ViewStyle;
}

function Hero({children, style}: Props) {
  
  return (
    <View style={[style, styles.hero]}>
      <ImageBackground 
        source={require('./../public/images/segundo-login.jpg')} style={styles.backgroundImage}>
          <Image
            style={[styles.logo]}
            source={require('./../public/images/segundo-logo.png')}
          />
          <View>
            <Text style={{ color:'#fff', maxWidth: '50%', textAlign: 'center'}}>
            Give away or sell unwanted items instantly to your community
              </Text>
          </View>
          <View style={{ marginTop: 30 }}>
            <Facebook />
            <Google />  
          </View>     
      </ImageBackground>
      
      {/* <SafeAreaView style={styles.absolute}>{children}</SafeAreaView> */}
    </View>
  );
}

const styles = StyleSheet.create({
  hero: {
    flex:2,
  },
  absolute: {
    flex: 1,
    position: 'absolute',
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
  },
  backgroundImage: {
    width: '100%',
    height: '100%',
    flex:1,
    flexDirection:'column',
    justifyContent:'center',
    alignItems:'center'
  },
  logo: {}
});

export default Hero;

and this is my App.tsx

import React, {useEffect, useState, createContext, ReactNode} from 'react';
import {Provider} from 'react-redux';
import {ReactReduxFirebaseProvider} from 'react-redux-firebase';
import auth, {FirebaseAuthTypes} from '@react-native-firebase/auth';
import Main from './Main';
import {store, rrfProps} from './redux/createStore';

interface Props {
  userData: any;
}

export const UserContext = createContext(null);

const App = ({userData}: Props) => {
  const [initializing, setInitializing] = useState(true);
  const [user, setUser] = useState(null);
  const [listenUser, setListenUser] = useState(false);

  /** Listen for auth state changes */
  useEffect(() => {
    const authListener = auth().onAuthStateChanged(result => {
      setUser(result);
      if (initializing && !listenUser) {
        setInitializing(false);
        setListenUser(true);
      }
    });

    return () => {
      if (authListener) {
        authListener();
      }
    };
  }, [initializing, listenUser]);

  /** Listen for user changes */
  useEffect(() => {
    let userListener: () => void;

    if (listenUser) {
      userListener = auth().onUserChanged(result => {
        setUser(result);
      });
    }

    return () => {
      if (userListener) {
        userListener();
      }
    };
  }, [listenUser]);

  const container = (children: ReactNode | ReactNode[]) => {
    return (
      <Provider store={store}>
        <ReactReduxFirebaseProvider {...rrfProps}>
          <UserContext.Provider value={user}>{children}</UserContext.Provider>
        </ReactReduxFirebaseProvider>
      </Provider>
    );
  };

  // if (user == null) {
  //   return null;
  // }
  return container(<Main />);
};

export default App;

I read about this error that it's probably related to a promise that is fulfilled after the component was unmounted.
Can you please help show me where is this coming from ?

@robbiew
Copy link

robbiew commented Jan 4, 2020

Getting the same error. Seems like a pretty big dealbreaker?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants