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

112
Visualizações
React component not rerendering even if updating state

Im currently using wavesurfer.js to play an audio file. I have created a useRef for the wavesurfer. On first load, I create an instance of WaveSurfer and assign it to the wavesurferRef. I am currently running this code as well

  const [timeRemaining, setTimeRemaining] = useState('')

  useEffect(() => {
    if (playing && wavesurfer.current) {
      wavesurfer.current.on('audioprocess', function () {
        if (wavesurfer.current?.isPlaying()) {
          const totalTime = wavesurfer.current.getDuration()
          const currentTime = wavesurfer.current.getCurrentTime()
          setTimeRemaining(`-${Math.round(totalTime - currentTime)}`)
        }
      })
    }
  }, [playing])

From my understanding of useState. The component should rerender everytime state changes so assume that since setTimeRemaining is called for every millisecond then the Component should rerender but when I check the logs. The useEffects that loads on component mount is not rerunning.

Why isnt my component rerendering?

Ideally, instead of useState, I should use useRef for the timeRemaining value but when I use useRef, the div that displays the value of timeRemaining doesnt show the new value of timeRemaining ref.

Here is a stackblitz example containing an example for both the usestate and useref approach

https://stackblitz.com/edit/react-ts-kcrfuy?file=index.tsx

about 4 years ago · Juan Pablo Isaza
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