Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
React.js Animation of 3 components ends at the same time

I have 3 components with different numbers. What I want is something like this:

  1. All 3 numbers should increase for 2 seconds.
  2. I want this animation effect to end at the same time.
  3. I don't want to use another library.

What should I consider? here is my trial. I would appreciate it if you could give me some points to consider.

https://jsfiddle.net/pizza_hamburger_chicken/ptav9wyg/7/

const {useState, useEffect} = React;

const AnimationNumber = ({ number }) => {
  // ------------ useRef
  // ------------ useState
  const [numberForCount, setNumberForCount] = useState(0);
  // ------------ useEffect
  useEffect(() => {
    let count = 0;
    const id = setInterval(() => {
      if (count >= number) {
        clearInterval(id);
      } else {
        console.log(++count);
        setNumberForCount(count);
      }
    }, 0.01);
  }, []);
  // ------------ methods
  // ------------ event methods
  // ------------ api methods
  return (
    <div
      data-target={number}
    >
      <strong>
        <span>{numberForCount}</span>
      </strong>
      
    </div>
  );
};

function Example() {
  return (
    <div>
      <AnimationNumber number={700}  />
      <AnimationNumber number={100} />
      <AnimationNumber number={470}  />
    </div>
  );
}

ReactDOM.render( <Example />, document.getElementById('root') );
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

JSfiddle code

const {useState, useEffect, useCallback} = React;

const animationSpeed = 7

const useCounter = (ending, start) => {
  const [count, setCount] = useState(0)
  const ratio = parseInt(ending/100)
  useEffect(() => {
        if (start) {
        const interval = setInterval(() => {
        if (count < ending) {
          setCount(count+ratio > ending ? ending : count+ratio)
        } else {
            clearInterval(interval)
        }
        }, animationSpeed)
        return  () => clearInterval(interval)
      }
  }, [count, start])
  return count
}

const AnimationNumber = ({ number , start}) => {
  let count = useCounter(number, start)
  return (
    <div
      data-target={number}
    >
      <strong>
        <span>{count}</span>
      </strong>
      
    </div>
  );
};

function Example() {
    const [starter, setStarter] = useState(false)
    const start = useCallback(() => {
    setStarter(true)
  }, [])
  return (
    <div>
      <button onClick={start}>Start!</button>
      <AnimationNumber number={100} start={starter} />
      <AnimationNumber number={1500} start={starter} />
      <AnimationNumber number={5000} start={starter} />
      <AnimationNumber number={590} start={starter} />
      <AnimationNumber number={1024} start={starter} />
      <AnimationNumber number={3600} start={starter} />
    </div>
  );
}

ReactDOM.render( <Example />, document.getElementById('root') );
about 4 years ago · Juan Pablo Isaza Relatório

0

If the component's only purpose is to display this number then you could avoid state altogether and use a ref and update the span's innerText directly

JsFiddle

const {useState, useRef, useEffect} = React;

const _period = 10000

const AnimationNumber = ({ number, intervalTime=10, period=_period }) => {
  const countEl = useRef(null);
  const count = useRef(0);
  
  useEffect(() => {
    const interval = setInterval(() => {
      if (count.current < number) {
        const amountPerRun = number * (intervalTime/period);

        count.current += amountPerRun;

        countEl.current.innerText = Math.min(number, Math.floor(count.current));

        return;
      }

      clearInterval(interval)
      console.log('done!', number);
    }, intervalTime);
    
    return  () => clearInterval(interval)
  }, []);
  
  return (
    <div>
      <strong>
        <span ref={countEl} />
      </strong>
    </div>
  );
};

function Example() {
  return (
    <div>
      <AnimationNumber number={700}  />
      <AnimationNumber number={100} />
      <AnimationNumber number={470}  />
    </div>
  );
}

ReactDOM.render( <Example />, document.getElementById('root'));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda