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.

State update doesn't trigger countup to update

See original GitHub issue

I am trying to use this as a part of a live dashboard, but for some reason, state changes from parent components doesn’t trigger an update to useCountUp. If I use an actual number in the effect update, it triggers correctly, but state updates do not. Console logging shows that an update occured, but the counter does not adjust. Code example below:

const Data = props.data const { countUp, start, update } = useCountUp({ start: 0, end: Data(), delay: 0, duration: 1, onUpdate: () => console.log('Updated!'), }); useEffect(() => { update(Data())} ,[Data, update])

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
mmarkelovcommented, Oct 29, 2020

@wantToHawaii I know that it is something weird, but for this case you should use provided update function:

import React from "react";
import PropTypes from "prop-types";
import { useCountUp } from "react-countup";

const ChildComponent = ({ value }) => {
  const { countUp, update } = useCountUp({ start: 0, end: value, duration: 2 });

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

  return (
    <div>
      <div>value without countUp: {value}</div>
      <div>value with countUp: {countUp}</div>
    </div>
  );
};

ChildComponent.propTypes = {
  value: PropTypes.number.isRequired
};

export default ChildComponent;
0reactions
mmarkelovcommented, Jul 16, 2021

should be ok in react-countup@5.0.0-0

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Countup trigger start when state is true - Stack Overflow
I would like to find a way how to start is from outside of the Component. I can't figure it out how to...
Read more >
Bountysource
State update doesn't trigger countup to update.
Read more >
Why React doesn't update state immediately - LogRocket Blog
When developing React applications, you may have noticed that state updates don't immediately reflect new values after being changed.
Read more >
react-countup | Yarn - Package Manager
Complete example ; from 'react-countup' ; () => { const ; const { start, pauseResume, reset, update } = useCountUp({ ref ; 0,...
Read more >
react-countup-es - npm
Forces count up transition on every component update. Default: false. preserveValue: boolean. Save previously ended number to start every new ...
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