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

127
Visualizações
Use object to map color to component React

I am trying to create a reusable tag component and based on company will apply the correct background and text color.

const colorMapping = {
  facebook: "white"
}

const backgroundMapping = {
  facebook: "blue"
}

export const App = (props) => {
   return (
       <div style={{ backgroundColor: "", color: "" }}>
       {props.company} //Facebook
       </div>
   )
}

How can I apply the color based on the based string? and Is this the right approach, could I just use 1 mapping object?

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

0

How can I apply the color based on the based string? let say this.props.company name is the base string,right? if its so then you can do something like this

style={{ backgroundColor: backgroundMapping[this.props.company], color: colorMapping[this.props.company] }}

but,wait

Is this the right approach, could I just use 1 mapping object? no,then why?what if there is no color for given this.props.company? so you code may need to check first that for given company name mapping found,and of course you should use single mapping instead of two, then how? consider the steps create a theme object like this

const theme={
       facebook:{color:"while",background:"blue"},
       defaultTheme:{
            color:"while",background:"black"
        }
 }

and then you can assign this theme to your component as following

 <div style={theme[this.props.company]||theme.default}>
   {props.company} //Facebook
   </div>
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