Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

100
Vistas
React scroll event keeps on firing

I'm working on a react functional components project, wherein I've to increment scroll position programmatically when a user actually scrolls in a div.

for example, I've a div with id testDiv & a user scrolled down a bit, now the scroll position is 100, so I want to programmatically increment it by 1 and make it 101.

Problem statement: The scroll position keeps on incrementing via onScroll handler, so the scrollbar only stops at the end of the element even if we scroll only once.

Expected behaviour: Scroll position should be incremented only once by the onScroll handler if we scroll once on the UI.

What I tried: (dummy code for the reproduction purpose)

import React, { useCallback } from "react";
import ReactDOM from "react-dom";
    
const App = (props) => {
  const onScroll = useCallback((event) => {
    const section = document.querySelector('#testDiv');
    // **The problem is here, scrollTop keeps on incrementing even if we scrolled only once**
    if (section) section.scrollTop = event.scrollTop + 1;
  }, []);
  
  return (
    <div id="testDiv" onScroll={onScroll} style={{ height: "500px", overflowY: "scroll" }}>
      <div>test</div>
      <div className="forceOverflow" style={{height: 500 * 25}}></div>
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What you're looking for is throttling or debouncing the function. It keeps on incrementing because with every bit of scroll, onScroll is being called.

There are many ways to throttle functions in react but I like to use debounce from lodash. If I remember correctly it was about 1.5kb gzipped.

I made you a sandbox. And here is the code:

import React, { useCallback } from "react";
import _debounce from "lodash/debounce";

export const App = (props) => {
  let debouncedOnScroll = useCallback(
    _debounce(() => {
      // YOUR CODE HERE
      console.log("CHECK THE CONSOLE TO SEE HOW MANY TIMES IT'S GETTING CALLED");
    }, 150), []); // Wait 150ms to see if I'm being called again, if I got called before 150ms, don't run me!

  return (
    <div
      id="testDiv"
      onScroll={debouncedOnScroll}
      style={{ height: "500px", overflowY: "scroll" }}
    >
      <div>test</div>
      <div className="forceOverflow" style={{ height: 500 * 25 }}></div>
    </div>
  );
};

By the way, use useRef API instead of document.querySelector. This query selector is getting called with every scroll and it's not the lightest weight on the client computer.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda