question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`abort()` works, but still get "Warning: Can't perform a React state update on an unmounted component". CodeSandbox provided

See original GitHub issue

When returning abort as the useEffect cleanup function, the request aborts and the promise resolves as expected, but I still see the React warning: 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. in Test (at App.js:15)

Question: How do I cancel a fetch on unmount (and not receive the React warning)?

In case this Codesandbox link expires, I’ll paste the code below. I tried to keep it simple – one component unmounts another component before its fetch finishes

App.js - unmount <Test /> after 100ms (to interrupt the API call made by <Test />)

import Test from "./Test";
import "./styles.css";

export default function App() {
  const [bool, setBool] = useState(true);

  // 100ms after mounting, set bool to false
  useEffect(() => {
    setTimeout(() => {
      setBool(false);
    }, 100);
  }, []);

  return <div className="App">{bool && <Test />}</div>;
}

Test.js - Make an API call using useFetch, and return abort from useEffect

import useFetch from "use-http";

export default function Test() {
  const { get, abort } = useFetch(
    "https://jsonplaceholder.typicode.com/posts/1"
  );

  const loadIt = async () => await get();

  useEffect(() => {
    loadIt();

    return abort;
  }, []);

  return <h1>I am here</h1>;
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
alex-corycommented, Mar 23, 2020

This should be fixed with v0.4.0. Let me know if the issue persists and I’ll reopen this.

0reactions
nth-chilecommented, Mar 20, 2020

Awesome. I will check it out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-hooks. Can't perform a React state update on an ...
I get this error: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory...
Read more >
Can't perform a React state update on an unmounted ... - GitHub
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....
Read more >
React state update on an unmounted component - debuggr.io
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...
Read more >
Avoid React state update warnings on unmounted components
React raising a warning when you try to perform a state update on an unmounted component. React setState is used to update the...
Read more >
Avoid Memory Leak With React SetState On An Unmounted ...
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 ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found