0

I have the common warning displaying upon loading of my web app but never again...

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

EDIT**** It is caused by this chunk of code. I have narrowed it down to one function. It blows up when I try to setMoisture state. I am not sure why.

function getData (){
    Axios.get("http://localhost:3001/api/get-value").then((response) => {
        const recievedData = response.data;
        const dataValue = recievedData.map((val) => {
            return [val.value]
        })
        if (loading === true){
            setLoading(false);
        }
        return parseInt(dataValue);
    }).then((resp)=>setMoisture(resp))
}

React.useEffect(() => {
    if (moisture === "initialState"){
        getData();
    }
}, []); 
8
  • I think I have narrowed down the issue to within the getData function. Commented Dec 9, 2020 at 22:57
  • what does setMoisture() do? Any components used there? Commented Dec 10, 2020 at 0:06
  • Yes it is a state variable [moisture, setMoisture] = useState[''] Commented Dec 10, 2020 at 0:07
  • I have eddited my previous post. This is what is causing the issue. Any idea how to fix this? I am calling setLoading also which is a state variable and no issues Commented Dec 10, 2020 at 0:08
  • 1
    Have you seen this question yet? I think your situation is similar. stackoverflow.com/questions/56442582/… Make sure to return a function that does the cleanup towards the end of useEffect. Something like return () => { ... }; Commented Dec 10, 2020 at 0:22

1 Answer 1

1

Posting the answer here (based from the comments) for completeness.

Basically, use local variables and cleanup function towards the end of useEffect(). Using this as reference:

Similar situation here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.