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

291
Visualizações
React App freeze caused by function constantly running

This page of my react app contains the createBubbles function, which works ok. The problem is that when im going to another page after visiting this one, the whlole app freeze.

This is the errors that console shows, i tried with window.onloand but doesnt work.

 function createBubbles(){
    const section = document.querySelector('section');
    const createElement = document.createElement('span');
    var size = Math.random() * 60;

    createElement.style.width = size + 'px';
    createElement.style.height = size + 'px';
    createElement.style.left = Math.random() * innerWidth + 'px';
    window.onload=section.appendChild(createElement);
    setTimeout(() => {
      createElement.remove();
    },4000)
  }
  setInterval(createBubbles,50);

BrowserConsoleErrors

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you need to clear your interval on component unmount.

const interval = setInterval(createBubbles,50);

useEffect(() => {
    return () => {
        clearInterval(interval)
    }
}, [])
about 4 years ago · Juan Pablo Isaza Relatório

0

That error happens since when you unmount that component by navigating to another Component route, your Event Loop queue is still full of functions to be executed since the setInterval is still active, but the DOM node that you are selecting to append the bubbles no longer exists.

  • When using setTimeout, event listeners, setInterval, etc... in React, you need to place the execution inside a useEffect and you must make sure to clean the interval, event listener, timeout, etc... on unmount, you achieve that by returning from useEffect a function that performs the clear:

  • To use DOM nodes directly, don't use vanilla JS selectors but use a ref.

     export default function App() {
       const sectionRef = useRef();
       useEffect(() => {
         function createBubbles() {
         const section = sectionRef.current;
         const createElement = document.createElement('span');
         const size = getRandom(60);
         const innerWidth = window.innerWidth;
         const innerHeight = window.innerHeight;
    
         createElement.style.backgroundColor = 
         `rgba(${getRandom(255)},${getRandom(255)},${getRandom(255)})`;
         createElement.style.width = size + 'px';
         createElement.style.height = size + 'px';
         createElement.style.left = Math.random() * innerWidth + 'px';
         createElement.style.top = Math.random() * innerHeight + 'px';
    
         if (section) {
           section.appendChild(createElement);
    
           setTimeout(() => {
           createElement.remove();
           }, 4000);
           }
         }
         const interval = setInterval(createBubbles, 50);
         return () => clearInterval(interval);
         }, []);
         return <section ref={sectionRef}></section>;
       }
    
       function getRandom(max) {
         return Math.random() * max;
        }
    

A working example https://stackblitz.com/edit/react-959lni?file=src%2FApp.js

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