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

192
Vistas
How to pass any anonymous function to ref and use that in other react component?

I have 3 functional components and I'd like to pass & manipulate data through useRef.
I am adding this onClose method to current property in one of my component.

const onClose = () => {
    setButtonColor(buttonColor);
};

ref.current = {
    clearSwitchStateOnClose: onClose,  
};

I am calling that method in another component.

ref.current.clearSwitchStateOnClose();

I am receiving this error,

Uncaught TypeError: ref.current.clearSwitchStateOnClose is not a function
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I was calling this method ref.current.clearSwitchStateOnClose(); but with that I was also mutating ref. I guess that was causing the issue.
It was something like this,

ref.current = {
    showModal: false,
    modalResponse: null,
};
ref.current.clearSwitchStateOnClose();

After doing this, it is working fine.

ref.current.clearSwitchStateOnClose();
ref.current = {
    showModal: false,
    modalResponse: null,
};

Sorry for not sharing the entire scenario of this situation.

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