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

193
Visualizações
React: How to have a onClick handler on parent affect children?

Originally I had a Parent Component which had this wrapper component on some DOM elements:

const Card = ({ }) => {

  const LinkComponent = ({ children }) =>
    to && !onClick ? (
      <Link aria-label={`${title} ${t('thumbnail-image')}`} to={to}>
        {children}
      </Link>
    ) : (
      <a aria-label={`${title} ${t('thumbnail-image')}`} href={href} onClick={onLinkClick}>
        {children}
      </a>
    );

  return (
    <CardArticle id={id} isListView={isListView} data-testid="card-article">
      {showThumbnail && (
        <LinkComponent>
          <CardImage
            id={id}
            alt={title}
            image={image}
            isLazyLoading={isLazyLoading}
            isListView={isListView}
          />
        </LinkComponent>
      )}
      <CardInfo>
        <TitleWrapper>
            <LinkComponent>
              <CardTitle data-testid="card-title" tabIndex={0} aria-label={title || t('untitled')}>
                {title}
              </CardTitle>
            </LinkComponent>
        </TitleWrapper>
    </CardArticle>
  );
};

What I want is instead of wrapping these elements with a component, I'd just like to pass a function to the onClick handler of the parent of the children.

const handleOnClick (e) => {
  if (to && !onClick) {
   return <Link aria-label={`${title} ${t('thumbnail-image')}`} to={to}>
           {xxxxxx} // what goes here?
          </Link>
  } else {
 return <a aria-label={`${title} ${t('thumbnail-image')}`} href={href} onClick={onLinkClick}>
        {xxxxxx} // what goes here?
       </a>
 }
}

  return (
    <CardArticle onClick={handleOnClick}>
    </CardArticle/>

I need to make the entire component a link instead of sections like it was!

Any help would be appreciated!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

if you can edit CardArticle component you can do it in such way:

// CardArticle file
const CardArticle = ({children, onClick}) => {
  return onClick ? onClick(children) : children
}
const Card = ({ }) => {
  const handleOnClick = (children) => {
   if (to && !onClick) {
    return <Link aria-label={`${title} ${t('thumbnail-image')}`} to={to}>
           {children} // what goes here?
          </Link>
    } else {
 return <a aria-label={`${title} ${t('thumbnail-image')}`} href={href} onClick={onLinkClick}>
        {children} // what goes here?
       </a>
   }
  }

  return (
    <CardArticle onClick={handleOnClick}>
    </CardArticle>
    )
}

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