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

313
Vistas
Avoid inline styling - Mui V5

In my opinion seperating styling from code makes the code clearer and cleaner. Using inline styling (style={{}}) has been considered a bas practice for me.

In Mui V4 is was easy - I've just created a styles file and imported it to my component code using makeStyles() and useStyles() hooks.

Now with the new sx prop in Mui V5 I have no idea how to avoid inline styling. Any ideas?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can do something similar with sx too. Create a new styles file and define your objects there. Just use them in the sx attribute :

styles.ts:

const gridStyles = {
  backgroundColor: "blue",
  paddingBottom: 2,
  paddingRight: 2,
  marginTop: 2,
  marginLeft: "auto",
  marginRight: "auto",
  maxWidth: 500
};
export default gridStyles;
import gridStyles from './styles';

export default function FullWidthGrid() {
  return (
    <>
      <Grid
        container
        sx={gridStyles}
      </Grid>
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

to respond to @Yahli Gi, if u want to access the theme, there's two ways:

Method one:

const gridStyles = (theme) => ({
  backgroundColor: "blue",
  paddingBottom: 2,
  paddingRight: 2,
  marginTop:theme.spacing(1),
  marginLeft: "auto",
  marginRight: "auto",
  maxWidth: 500
});

export default gridStyles;

Method Two:

const gridStyles = {
  backgroundColor: "blue",
  paddingBottom: 2,
  paddingRight: 2,
  marginTop: (theme) => theme.spacing(1),
  marginLeft: "auto",
  marginRight: "auto",
  maxWidth: 500
};

export default gridStyles;

The imports are the same for the two examples above. Additionally the theme is automatically injected into the object by mui so no need to call the function yourself in the import.

import gridStyles from './styles';

export default function FullWidthGrid() {
  return (
    <>
      <Grid
        container
        sx={gridStyles}
      </Grid>
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar
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