It appears to be possible to trigger a React "Can't perform a React state update on an unmounted component" error based on a very specific set of circumstances involving a function component that queues a state update while re-rendering, ala "how do you implement getDerivedStateFromProps with hooks?".
The behavior was reported by @samselikoff, who narrowed it down to this repro project:
I then investigated the behavior he reported and was asking about.
The home page component looks like this:
const fetcher = (url) => fetch(url).then((res) => res.json());
function Home() {
const { data } = useSWR("/api/tweets", fetcher);
let [buffer, setBuffer] = useState();
if (data && !buffer) {
setBuffer(data);
}
return <p>lorem ipsum</p>;
}
Home.headerTitle = "Latest Tweets";
Home.headerBorder = true;
export default Home;
When toggling back and forth between tabs, this component gets mounted and unmounted repeatedly. At some point, SWR tries to set state on a given instance of this component after it has been unmounted, causing that error to be thrown.
The error goes away if the setBuffer() call is put into either useEffect or useLayoutEffect. That said, a state setter here _is_ semantically legal ala gDSFP, and the error is _only_ thrown if that state setter is there.
SWR will never try to set state on an unmounted component,
It'll probably take several tries, but it should eventually throw that "unmounted" error.
Chrome will show a very long async callstack, which includes use-swr.js:326 (built ESM version) where it's doing dispatch(newState); Checking the unmountedRef value at that point in time shows true:

Inspecting the code, I see that SWR is calling requestIdleCallback. Something about the combination of that extra state setter causing an additional run of a render cycle, plus the timing of the idle callback, is causing SWR to try to set state after the instance unmounted.
My guess is that this particular revalidation function needs to check unmountedRef.current a bit earlier and bail out if necessary.
SWR version: 0.2.3
hi @marcelmokos , thanks for reporting! I belive it's the similar one to #494 , has been fixed in #510 .
I verified that master has the fix + the problem went away in my code! Thank you 馃槃
Most helpful comment
I verified that master has the fix + the problem went away in my code! Thank you 馃槃