Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda