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

287
Visualizações
TypeError: Cannot set properties of undefined (setting 'transform')

This is the part of my react application, where I've used a ref.current which is equal to null, then I'm changing it's property, I know like this is surely gonna give me error but in one of my friend's project with same react component WorkPage it's working fine. I'm so confused, is there any sort of fault in mine?

const WorkPage = () => {
  const ref = useRef(null);

  useEffect(() => {
    let element = ref.current;

    const rotate = () => {
      element.style.transform = `translateX(${-window.pageYOffset}px)`
    };

    window.addEventListener('scroll', rotate)
    return () => window.removeEventListener('scroll', rotate)
  }, [])
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Surely it will throw error, because you did not assign ref to any DOM element, so ref.current is always null. Make sure to assign it to a DOM element like this:

const WorkPage = () => {
  const ref = useRef(null);

  useEffect(() => {
    let element = ref.current;

    const rotate = () => {
      element.style.transform = `translateX(${-window.pageYOffset}px)`;
    };

    window.addEventListener("scroll", rotate);
    return () => window.removeEventListener("scroll", rotate);
  }, []);

  // assign ref to one of your DOM element
  return <div ref={ref}>your element</div>;
};


about 4 years ago · Juan Pablo Isaza Relatório

0

refs is not guaranteed to be set on first render caveats-with-callback-refs so you need to check if the element exists before you try to manipulate it directly in the DOM

if (ref && ref.current) {
  // do something
}
about 4 years ago · Juan Pablo Isaza Relatório

0

On first render ref is undefined. You can use optional chaining operator

let element = ref?.current;

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