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.

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

See original GitHub issue

Hello,

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:closed
  • Created 5 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
reakaleekcommented, Feb 17, 2019

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.

0reactions
ghostcommented, Dec 19, 2020

Hi, sorry to resurrect this issue but I’m experiencing it on the latest version.

component is essentially

const MyComponent = () => {
  const value = useSelector(MySelector)
  const { countUp, update } = useCountUp({ start: value, end: value, duration: 0.75 })

  useEffect(() => {
    update(value)
  }, [update, value])

  return <div>{countUp}</div>
}

I’ve tried some silly things like wrapping update in a setTimeout and clearing in the return, and just returning the reset function, but neither worked.

delay properties need to be set to a suitable value

Read more comments on GitHub >

github_iconTop 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 >

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