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

177
Visualizações
Can't access Ref from useEffect using "this.refname.current"

I'm missing something simple here, I need to access a defined Ref within a useEffect . The debugger shows that this is undefined and therefore I can't do this.refOverlay.current;.

// Ref
const refOverlay = useRef(null); 

// Sample useEffect that references it
useEffect(() => {
    if (props.mode === 'new') {
        let b = this; // Undefined
        let a = this.refOverlay.current; // Error on this line
        if (a) {
            console.log('ok');
        }
    }
}, [props.mode]);

...
return (
{/* JSX Definition */}
        <OverlayTrigger ref={refOverlay} trigger="click" rootClose placement="bottom" overlay={popoverApprover}>
            <a href="javascript:void(0)" className="more-info">TEST</a>
        </OverlayTrigger> 
);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You have defined a functional based component. Which doesn't have the this Simply remove the this keyword and you're fine.

about 4 years ago · Juan Pablo Isaza Relatório

0

There's no such this in the function component.

  const Title = () => {
    const ref = useRef(null)
    
    useEffect(() => {
      if (ref.current) {
        ref.current.WHATEVERYOUWANTHERE
      }
    }, [])
  }

NOTE: couple of difference between class and function component

  • function component has no this, it's a function
  • function component is only the render function
  • to have state, you need useState
  • to have any persistent storage, you need hook
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