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

200
Visualizações
Best practices of using theme colors: string, variable or reference from theme object?

I have eslint-plugin-sonarjs installed and it yells at me about me using the same string - color from palette - over and over again. It suggests me to write it to a variable and use the variable instead.

What would be the best practice:

  1. Ignore eslint warning/disable it and use string names as usual
// MyComponent.tsx
...
<Typography color={'primary.white'}/>
...
  1. Write all theme colors in variables and use variables instead
// colors.ts
export const WHITE_COLOR = 'primary.white'
// rest of the colors
...

// MyComponent.tsx
...
<Typography color={WHITE_COLOR} />
...
  1. Use color from theme object
// MyComponent.tsx
const theme = useTheme()
...
<Typography color={theme.palette.primary.white} />
...
  1. Create hook usePalette that would return palette object from theme
// usePalette.ts
const theme = useTheme();
return useMemo(() => theme.palette, [theme]);

// MyComponent.tsx
const palette = usePalette();
...
<Typography color={palette.primary.white} />
...
  1. Or create a new Typography variant that uses the color
// MuiTypography.ts variants
...
{
  props: {variant: 'white-text'},
  styles: (theme) => ({
    color: theme.palette.primary.white
  })
}
...

// MyComponent.tsx
...
<Typography variant={'white-text'} />
...

?

In my opinion options 2) and 3) are ridiculous. 2) creates unnecessary wrapping and 3) just doesn't feel right, too much to write.
1) is the sweetiest.

What do you think is the best practice?

about 4 years ago · Juan Pablo Isaza
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