简体   繁体   English

制作 Styles 反应 JS

[英]Make Styles React JS

I have this problem.我有这个问题。 I'm trying to work with material-ui for my web design, I have this code:我正在尝试为我的 web 设计使用 material-ui,我有这个代码:

const useStyles = makeStyles((theme) => ({
  card: {
    marginTop: theme.spacing(10),
    direction:"column",
    alignItems:"center",
    justify:"center",
    borderRadius: 30,
    boxShadow: ' 0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22)',
    },
  contenidoFormulario: {
    marginTop: theme.spacing(8),
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
  },
  avatar: {
    margin: theme.spacing(1),
    backgroundColor: theme.palette.primary.main,
  },
  formulario: {
    width: '60%',
    marginTop: theme.spacing(1),
  },
  botonIngresar: {
    margin: theme.spacing(3, 0, 2),
    borderRadius: 50,
  },
}));

And I'm using it in three diferent components (Login.js, Newaccount.js, Forgotpassword.js), how do I re-use it to avoid repeating the same code?而且我在三个不同的组件(Login.js、Newaccount.js、Forgotpassword.js)中使用它,如何重复使用它以避免重复相同的代码?

You can create a file like commonStyles.js and export your style.您可以创建一个类似于commonStyles.js的文件并导出您的样式。 In other components, you just need to import this style and call it inside your component.在其他组件中,您只需要导入此样式并在您的组件中调用它。 Read more about combine styles in MUI via this issue and the official document .通过本期官方文档阅读更多关于在 MUI 中结合 styles 的信息。 Try this:尝试这个:

In commonStyles.js :commonStyles.js

 ... export const commonStyles= makeStyles((theme) => ({ card: { marginTop: theme.spacing(10), direction:"column", alignItems:"center", justify:"center", borderRadius: 30, boxShadow: ' 0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22)', }, contenidoFormulario: { marginTop: theme.spacing(8), display: 'flex', flexDirection: 'column', alignItems: 'center', }, avatar: { margin: theme.spacing(1), backgroundColor: theme.palette.primary.main, }, formulario: { width: '60%', marginTop: theme.spacing(1), }, botonIngresar: { margin: theme.spacing(3, 0, 2), borderRadius: 50, }, }));

In Login.js :Login.js

 import {commonStyles} from '../commonStyles' import {makeStyles} from '@material-ui/core/styles' const useStyles = makeStyles(theme => ({... })) const Login = () => { const classes = useStyles() const commons = commonStyles() return ( <div className={commons.card}>... </div> ) } export default Login

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM