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

woof

Greetings I'm trying to debug some code right now, not sure how to fix this useEffect that I have in my header to detect for screen size view. Here is the error I'm getting for the useEffect:

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.

My code:

const [state, setState] = useState({
    mobileView: false,
    drawerOpen: false,
});

const { mobileView, drawerOpen } = state;

useEffect(() =>
{
    const setResponsiveness = () =>
    {
        return window.innerWidth < 1000
            ? setState((prevState) => ({ ...prevState, mobileView: true }))
            : setState((prevState) => ({ ...prevState, mobileView: false }));
    };

    setResponsiveness();

    window.addEventListener("resize", () => setResponsiveness());
}, []);

The dependency array is already empty, im not sure what else to put in there though?

hussain.codes

It's not useEffect that is causing the problem, it's window resize event which is being excuted whenever window size changes.

You are adding resize event to window when your function mounts. but when you unmount the component, resize event is still listening and whenever window size changes your setResponsiveness function will be called. and setResponsiveness will try to update the state values of unmounted component. So basically your functional component is leaking memory because even though it has unmounted setResponsiveness is still attached to as a callback to windo resize event.

You should always remove component specific event listener either in componentWillUnmount or in useEffect return function, depending on component type.

const [state, setState] = useState({
    mobileView: false,
    drawerOpen: false,
});

const { mobileView, drawerOpen } = state;

function setResponsiveness() {
    return window.innerWidth < 1000
        ? setState((prevState) => ({ ...prevState, mobileView: true }))
        : setState((prevState) => ({ ...prevState, mobileView: false }));
}

useEffect(() => {
    setResponsiveness();
    window.addEventListener("resize", setResponsiveness);
    // return a cleanup function which will remove the event listener
    return () => {
        window.removeEventListener("resize", setResponsiveness);
    };
}, []);



Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React useEffect causing: 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

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

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

React-Native: Warning: Can't perform a React 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

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

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

React can't perform state update on unmounted component

Can't perform a React state update on an unmounted component in React js and firestore

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

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

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

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

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

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

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

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

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

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 perform a React state update on an unmounted component useEffect error

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

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

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

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

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

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