Can't perform a React state update on an unmounted component theme provider

JohnSmith

I need help because I get the following error: 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 the componentWillUnmount method. in createCategory (at themeProvider.js:39)

/* Imports */
    import React, { useContext, useState, useEffect } from 'react';
    import AsyncStorage from '@react-native-community/async-storage';
    import THEMES from '@app/theme/themes.json';
    /* /Imports/ */

    const STORAGE_KEY = 'THEME_ID';
    const ThemeContext = React.createContext();

    /* Exports */
    export const ThemeContextProvider = ({ children }) => {
      const [themeID, setThemeID] = useState();

      useEffect(() => {
        (async () => {
          const storedThemeID = await AsyncStorage.getItem(STORAGE_KEY);
          if (storedThemeID) setThemeID(storedThemeID);
          else setThemeID(THEMES[1].key);
        })();
      }, []);

      return (
        <ThemeContext.Provider value={{ themeID, setThemeID }}>
          {!!themeID ? children : null}
        </ThemeContext.Provider>
      );
    };

    export function withTheme(Component) {
      function TargetComponent(props) {
        const { themeID, setThemeID } = useContext(ThemeContext);
        const getTheme = themeID => THEMES.find(theme => theme.key === themeID);
        const setTheme = themeID => {
          AsyncStorage.setItem(STORAGE_KEY, themeID);
          setThemeID(themeID);
        };

        return (
          <Component
            {...props}
            themes={THEMES}
            theme={getTheme(themeID)}
            setTheme={setTheme}
          />
        );
      }

      TargetComponent.navigationOptions = Component.navigationOptions;

      return TargetComponent;
      }
    /* /Exports/ */
Jurrian

If you don't already know - you can return a function at the end of your useEffect hook. That function will be called whenever that effect is fired again (e.g. when the values of its dependencies have changed), as well as right before the component unmounts. So if you have a useEffect hook that looks like this:

useEffect(() => {
  // logic here

  return () => {
    // clean up
  };
}, []); // no dependencies!

Is equivalent to this:

class SomeComponent extends React.Component {
  componentDidMount() {
    // logic here
  }

  componentWillUnmount() {
    // clean up
  }
}

So in your code I'd add this:

useEffect(() => {
  let isCancelled = false;
  const fetchData = async () => {
    try {
      // fetch logic omitted...
      const data = await AsyncStorage.getItem(STORAGE_KEY);

      if (storedThemeID) setThemeID(storedThemeID);
      else setThemeID(THEMES[1].key);
    } catch (e) {
      throw new Error(e)
    }
  };

  fetchData();

  return () => {
    isCancelled = true;
  };
}, [themeID]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React Can't perform a React state update on an unmounted component error

Can't perform a React state update on an unmounted component in react

React - Can't perform a React state update on an unmounted component

React Spring "Can't perform a React state update on an unmounted component"

React: Can't perform a React state update on an unmounted component

can't perform a react state update on an unmounted component issue with useEffect

useEffect - Can't perform a React state update on an unmounted component

React can't perform state update on unmounted component

Can't perform a React state update on an unmounted component in reactjs

Can't perform a React state update on an unmounted component

Can't perform a React State update on unMounted child component?

Getting Warning: Can't perform a React state update on an unmounted component

Can't perform a react state update on an unmounted component error

Warning: Can't perform a React state update on an unmounted component

React form submit and "can't perform state update on an unmounted component"?

ReactJS & Redux: Can't perform a React state update on an unmounted component

Can't perform a React state update on an unmounted component?

Fix "Can't perform a React state update on an unmounted component" error

ReactJS : Can't perform a React state update on an unmounted component

arning: Can't perform a react state update on an unmounted component

Can't perform a React state update on an unmounted component useEffect error

Can't perform a React state update on an` unmounted` component

Can't perform a React state update on an unmounted component, using class component and component did mount

React testing library: An update inside a test was not wrapped in act(...) & Can't perform a React state update on an unmounted component

Can't get rid of: Warning: Can't perform a React state update on an unmounted component

Warning: Can't perform a React state update on an unmounted component. In a functional component

React Native - Can't perform a React state update on an unmounted component, useEffect cleanup function

React useEffect causing: Can't perform a React state update on an unmounted component

React-hooks. Can't perform a React state update on an unmounted component