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

172
Vistas
Error Getting Component Position with ref (ReactCannot read property 'getBoundingClientRect' of null")

I want to get the position of the Skills component and add an animation when the scroll reaches to that point. However, it returns an error "TypeError: Cannot read property 'getBoundingClientRect' of null". I followed tried to follow this blog but I don't know what I am doing wrong. Can somebody help me?

Card.js

import classes from './Card.module.css';
import React from 'react';

const Card = React.forwardRef((props, ref) => {
  return (
    <section
      className={`${classes.card} ${props.className}`}
      id={props.id}
      ref={ref}
    >
      {props.children}
    </section>
  );
});

export default Card;

Skills.js

const Skills = () => {
  const ref = React.createRef();
  const topPos = ref.current.getBoundingClientRect().top;

  const onScroll = () => {
    const scrollPos = window.scrollY + window.innerHeight;
    if (topPos > scrollPos) {
      // enter animation code here
    }
  };

  useLayoutEffect(() => {
    window.addEventListener('scroll', onScroll);
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  return (
    <Card className={classes.skills} id='skills' ref={ref}>
      <H2>Skills</H2>
      <div className={classes['skills-list-container']}>
        <div className={classes['skills-list']}>{skillsList}</div>
      </div>
    </Card>
  );
};

export default Skills;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are reference to ref.current before it gets attached to element

After creating a ref, you will need to wait for the initial render, then that ref will be attached to an actual element.

By doing like above, you are accessing ref.current before the initial render result in a null.

So just move it inside your onScroll function like so:

const onScroll = () => {
  const topPos = ref.current.getBoundingClientRect().top;  <-- MOVE HERE
  const scrollPos = window.scrollY + window.innerHeight;
  if (topPos > scrollPos) {
    // enter animation code here
  }
};

Moving your ref.current to eventHandler body because that handler is only being triggered after the component is completely rendered so ref also already be attached.

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