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

206
Visualizações
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 à 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