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

153
Visualizações
Reducing Repetitive Code Within Styled-components

I'm using styled-components in a react project, and wondering if there's a way to reduce these 3 blocks that are repeating, with the only difference being the color of the border.

Initially, I'm thinking of creating an object that maps out the possible alert values (success, warning, danger), and add appropriate color as their keys, however I'm not sure where it to put that object.


const customStyles = {

  ${({ alert }) =>
    alert === "success" &&
    css`
      border: 1px solid green;
  `}

  ${({ alert }) =>
    alert === "warning" &&
    css`
      border: 1px solid orange;
  `}

  ${({ alert }) =>
    alert === "danger" &&
    css`
      border: 1px solid red;
  `}

}

const Input = styled.input`
  ${customStyles}
`;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

How about a function you can reuse that returns the right color based on the value?

Something like this:

function getAlertColor(alert){
  switch (alert) {
    case "success":
      return "green"
    case "warning":
      return "orange"
    case "danger":
      return "red"
  }
}

const Input = styled.input`
  border: ${props => `1px solid ${getAlertColor(props.alert)};`};
`;
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