Can't perform a React state update on an unmounted component.
See original GitHub issueHello,
So I’m having an issue with receiving a state update error on my component with the useCounter hook.
Here’s the class:
const SavingsCounter = ({ classes }) => {
let timerId = null;
const getCurrentMoneySaved = () => {
// returns number
};
const { countUp, update } = useCountUp({
start: getCurrentMoneySaved() - 245,
end: getCurrentMoneySaved(),
duration: 8,
separator: ',',
onReset: () => console.log('Resetted!'),
onUpdate: () => console.log('Updated!'),
onPauseResume: () => console.log('Paused or resumed!'),
onStart: ({ }) => console.log('start'),
onEnd: ({ }) => console.log('end'),
}, []);
const updateCounter = () => {
update(getCurrentMoneySaved());
// random timeout in 10 - 20 seconds
timerId = setTimeout(updateCounter, random(10000, 20000));
};
useEffect(() => {
updateCounter();
return function cleanup() {
clearTimeout(timerId);
};
}, []);
return (
<div className={classes.savingsWrapper}>
<Media query="(max-width: 960px)">
{
(matches) => {
const menu = matches ? (
<>
<Typography variant="h4" color="inherit">
$
{ countUp }
</Typography>
<Typography variant="h5" color="inherit">
Saved in Commissions
</Typography>
</>
) : (
<>
<Typography variant="h5" color="inherit">
Clever has saved homeowners
</Typography>
<Typography variant="h4" color="inherit">
$
{ countUp }
</Typography>
<Typography variant="h5" color="inherit">
in commissions and counting.
</Typography>
</>
);
return menu;
}
}
</Media>
</div>
);
};
Every time I leave the page I get the “state update on an unmounted component” error and then the logs: end end
It seems like something is holding onto a reference of the component and doing an update after the page is unmounted. I couldn’t find any way to unsubscribe from anything in react count up, and I’m canceling the timer I have set.
I’m using reach router if it makes any difference.
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Can't perform a React state update on an unmounted ...
Problem. I am writing an application in React and was unable to avoid a super common pitfall, which is calling setState(...) after ...
Read more >Can't perform a react state update on an unmounted component
To solve the "Warning: Can't perform a React state update on an unmounted component", declare an isMounted boolean in your useEffect hook that...
Read more >React: Prevent state updates on unmounted components
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 >Fix “Cannot perform a React state update on an unmounted ...
It happens if one performs a state update (call setState) on a component that has been unmounted. React warns us that this causes...
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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @bargangilo thank you for reporting this issue. I already created a pull request, which will fix the problem. We just need to wait for @glennreyes to publish the fix.
delay properties need to be set to a suitable value