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

155
Visualizações
JQuery Script in React component gets called after switching to other components with react-router

I have a JQuery script that should only be run on my home page, because it works with elements that only exist on home component

My home component:

import React, { Component, useState, useEffect } from 'react';
// Styles
import './assets/css/bootstrap.min.css';
import './assets/css/styles.css';
import './assets/css/swiper-bundle.min.css';
import './assets/css/default.min.css';
import ReactTextRotator from "react-text-rotator";
import $ from 'jquery';
// Highlight,js
import 'highlight.js';
import Highlight from 'react-highlight';
// Typed.js
import Typed from 'typed.js';
// Images
import uploadLaptop from './assets/images/uploadLaptop.png';
import logoDark from './assets/images/logoDark.png';
// Navigation
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import Wizard from './wizard';
// Script
import homeScript from './assets/js/home-js.js';

export default function Home() {
    const welcomePageTextToRotate = [
      {
        text: "Create"
      },
      {
        text: "List"
      },
      {
        text: "Hype",
      }
    ];
    return (
...

homeScript is the js script:

window.onload = function() {
  // Scroll
  var youInterval;

  function startInterval(){

      youInterval = setInterval(function() {
          var elem = document.getElementsByClassName('hljs')[0];
          elem.scrollTop = elem.scrollHeight;

      }, 500);

  }

  //If user scrolls, stop interval
  document.addEventListener('scroll', function (event) {
      if (event.target.id === 'scrollBottom') { // or any other filtering condition

          clearInterval(youInterval);
      }
  }, true /*Capture event*/);

  startInterval();
};

When I switch to wizard page that does not contain any scripts via react-router, it throws this error: TypeError: Cannot read properties of undefined (reading 'scrollHeight') which is the script used in home component.

How do I prevent this script being called on switch to other pages from home page?

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