Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!