heyman333 / react-animated-numbers

👾 Library showing animation of number changes in react.js
MIT License
246 stars 30 forks source link

Animate part of the number #59

Open e-Naeim opened 5 months ago

e-Naeim commented 5 months ago

is there a way to animate the number (e.g 111 => 112) so only last 1 becomes 2 instead of animating the whole number, because it doesn't make sense to animate numbers if they are not going to be changed.

is there a way to do that?

acfuns commented 4 months ago

pls use react-awesome-animated-number

cgzirim commented 4 months ago

@heyman333, can we get this please.

4Michael1Angelo5 commented 2 months ago

Hello. I may have a solution. You need to change part of the code to this.

`

  {
    NUMBERS.map((number, numIndex) => (
                  <motion.div
                    style={{ ...fontStyle, fontVariantNumeric: 'tabular-nums' }}
                    key={numIndex}
                    initial="hidden"
                    variants={{
                      hidden: { y: 0 },
                      visible: {
                        y: -1 * (numberHeight * animateToNumbersArr[index]) -  numberHeight * 20,
                      },
                    }}
                    animate={controls}
                    transition={transitions?.(index)}
                  >
                    {number}
                  </motion.div>
                ))
  }

`

the key changes are this line: NUMBERS.map((number) => to NUMBERS.map((number, numIndex) => and then you need to change this line: from key = {keyCount.current++} to key={numIndex}

Hope that helps

I found that creating the keys using keyCount.current++ led to a lot of unintended behavior and introduced bugs

I forked an example I made earlier that displayed bugs. Interestingly, this fix also fixed the bugs and unintentionally changed the animation style. I think this is the animation style you are looking for.

https://stackblitz.com/edit/vitejs-vite-xcgycu?file=src%2FApp.jsx