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

194
Visualizações
How can we pass a color as a prop?

I have a component which has the color white if it is active and black if it is inactive. How can a pass the color black as a prop?

My code right now (it does nothing):

export function Card (props) {
  return <div {...props} className={styles.gray} />
}

I want to use this component (called Card) in a ternary operator to show if the component is active or not.

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

0

Assuming that you are setting the active state outside of the card, here's the simple ternary pattern.

If active is defined outside of the card:

function Card({ isActive, ...props }) {
  return (
    <div
      className={`${styles.gray} ${isActive ? styles.active : null}`}
      {...props}
    />
  )
}

Using a helper like clsx, it would look like:

<div className={
  clsx(
    styles.gray,
    isActive ? styles.active : null,
  )
} />
about 4 years ago · Juan Pablo Isaza Relatório

0

Is styles the name of your css style sheet and gray the selector?

There are a bunch of different ways to create this behavior, but an easy one if you have a props object with color as a prop, you can do: <div style={{ color: props.color }}

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