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

365
Vistas
React .scrollLeft cannot be reading null value

I am trying to access the horizontal scroll bar with buttons in react but turns out it is not working because of error that cannot read the null values of .scrollLeft.

<>
  <div className="containerOuterSider">
    <FaAngleLeft className= "FaAngleLeft" onClick={slide('left')}/>
    <div id="container3" className="container3">
      {products &&
        products
          .map((product) => (
            <AAsOfLowNav key={product._id} product={product} />
          ))
          .reverse()}
    </div>
      <FaAngleRight className="FaAngleRight" onClick={slide('right')}/>
  </div>
</>

and the main function is

function slide(direction) {
  var container = document.getElementById('container3');
  let scrollCompleted = 0;
  var slideVar = setInterval(function() {
    if (direction === 'left') {
      container.scrollLeft -= 10;
    } else {
      container.scrollLeft += 10;
    }
    scrollCompleted += 10;
    if (scrollCompleted >= 100) {
      window.clearInterval(slideVar);
    }
  }, 50);
}

The error I am facing is:

Uncaught TypeError: Cannot read properties of null (reading 'scrollLeft') at LowerCatNav.js:31:1 Uncaught TypeError: Cannot read properties of null (reading 'scrollLeft') at LowerCatNav.js:33:1

How can I make it right? Should I use the hooks or anything else?

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

Issue

You are immediately invoking the slide function while rendering:

<FaAngleLeft
  className="FaAngleLeft"
  onClick={slide('left')} // <-- immediately invoked when rendered
/>
...
<FaAngleRight
  className="FaAngleRight"
  onClick={slide('right')} // <-- immediately invoked when rendered
/>

The React component hasn't been fully rendered and pushed to the DOM, so queries to the DOM, i.e. document.getElementById('container3'), return null.

Solution

Fix the click handler so slide is not being immediately invoked.

<FaAngleLeft
  className="FaAngleLeft"
  onClick={() => slide('left')} // <-- asynchronously invoked when clicked
/>
...
<FaAngleRight
  className="FaAngleRight"
  onClick={() => slide('right')} // <-- asynchronously invoked when clicked
/>

It is considered a React anti-pattern to directly query the DOM for DOMNodes, use a React ref for this.

Example:

...

const containerRef = React.useRef(); // <-- (1) create Ref
const sliderTimerRef = React.useRef();

useEffect(() => {
  return () => {
    // clear any running intervals on component unmount
    clearInterval(sliderTimerRef.current);
  };
}, []);

...

function slide(direction) {
  // clear any previously set intervals and reset scrollCompleted
  clearInterval(sliderTimerRef.current);
  let scrollCompleted = 0;

  sliderTimerRef.current = setInterval(function() {
    const container = containerRef.current; // <-- (3) access current ref value

    if (direction === 'left') {
      container?.scrollLeft -= 10; // <-- (4)  Optional Chaining null check
    } else {
      container?.scrollLeft += 10; // <-- (4)  Optional Chaining null check
    }
    scrollCompleted += 10;
    if (scrollCompleted >= 100) {
      clearInterval(sliderTimerRef.current);
    }
  }, 50);
}

...

return (
  <>
    <div className="containerOuterSider">
      <FaAngleLeft className="FaAngleLeft" onClick={() => slide('left')}/>
      <div
        ref={containerRef} // <-- (2) attach ref to element
        id="container3"
        className="container3"
      >
        {products
          .map((product) => (
            <AAsOfLowNav key={product._id} product={product} />
          ))
          .reverse()
        }
      </div>
      <FaAngleRight className="FaAngleRight" onClick={() => slide('right')}/>
    </div>
  </>
);
about 4 years ago · Santiago Gelvez Denunciar

0

  1. slide is called when #container3 is not yet created.
    Change onClick={slide(...)} to onClick={() => slide(...)}

  2. Access to global elements (e.g. by id) should be avoided.
    Prefer ref/createRef/useRef to access elements belonging to your component.

about 4 years ago · Santiago Gelvez 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