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

208
Vistas
React - Events doesn't fire when cloning functional components

I'm trying to develop a Tooltip component. To do so, I have to access the children (object that fires the tooltip) in order to get the events listeners.

I'm using React.cloneElement to achieve this, but the problem is this only works when the child is a DOM element, but doesn't work with functional components.

export const Tooltip = ({text, placement = 'top', space = 15, children, disabled = 0, delay = 0, ...props}) => {
  
  const [show, setShow] = useState(0);

  const handleMouseOver = () => {
    setShow(1); // shows the tooltip
    console.log('Mouse over');
  };

  const handleMouseOut = () => {
    setShow(0); // hides the tooltip
    console.log('Mouse out');
  };

  return (
    <>

      { React.cloneElement(children, {
        ...children.props,
        onMouseOver: handleMouseOver, //the events only fires if it is a DOM element
        onMouseLeave: handleMouseOut,
      })}

     { disabled ||
        <StyledTooltip delay={delay} ref={tooltipRef} posRef={posRef} show={show}>{text} 
        </StyledTooltip>
      }

    </>
  )
}

How could make it works whether the child is DOM element or functional component?

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
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