简体   繁体   English

如何在 React JS Material-UI 中使用 makeStyles 制作不同的 styles?

[英]How to make different styles using makeStyles in React JS Material-UI?

I am beginner to React JS and Material UI.我是 React JS 和 Material UI 的初学者。

I am trying to make different styles of buttons.我正在尝试制作不同的 styles 按钮。 For example if the button attribute name= submit,ok,cancel,confirm,alert, those each button's styles should be differ from one another例如,如果按钮属性名称= submit,ok,cancel,confirm,alert,则每个按钮的 styles 应该彼此不同

App.JS应用程序.JS

import CssButton  from './controls/Buttons/Button'

function App() {
  return (
    <div className="App">
      <CssButton name="btnSubmit"/> //button style to be changed based on 'name'
    </div>
  );
}

Button.JS按钮.JS

const useStyles = makeStyles({

    btnSubmit: {
        background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
        border: 0,
        borderRadius: 3,
        boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
        color: 'white',
        height: 48,
        padding: '0 30px',
    },
    ok:{
        background: 'blue',
        border: 0,
        borderRadius: 3,
        boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
        color: 'white',
        height: 48,
        padding: '0 30px',
    }
});

function CssButton(props) {
   const classes = useStyles(props);
   return<Button className={classes.btnSubmit}>{props.name}</Button>;
   //return <button className={`classes.${ props.name }`}>   --is it possible pass the props value something like this?
   // {props.name}
   //</button>
}

You should do this:你应该做这个:

const useStyles = makeStyles({

    btnSubmit: {
        background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
        border: 0,
        borderRadius: 3,
        boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
        color: 'white',
        height: 48,
        padding: '0 30px',
    },
    ok:{
        background: 'blue',
        border: 0,
        borderRadius: 3,
        boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
        color: 'white',
        height: 48,
        padding: '0 30px',
    }
});

function CssButton(props) {
   const classes = useStyles(props);
   return(
     <Button className={classes[props.name]}>
       {props.name}
     </Button>
   );
}

暂无
暂无

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

相关问题 Material-UI | 在 makeStyles 中使用`theme` - Material-UI | Using `theme` in makeStyles ./src/components/styles.js 尝试导入错误:&#39;@material-ui/core/styles&#39; 不包含默认导出(作为 &#39;makeStyles&#39; 导入) - ./src/components/styles.js Attempted import error: '@material-ui/core/styles' does not contain a default export (imported as 'makeStyles') makeStyles 的 Material-UI 问题 - Material-UI issue with makeStyles 如何使用 Material-UI 在 React 中迭代的信息制作 2 列 - How to make 2 columns with information that is iterated in React using Material-UI 如何为卡片制作可重复使用的组件以及如何使用 material-ui 的新 makeStyles? - How do I make a reusable component for the Cards and how can I use the new makeStyles of material-ui? 如何在使用材质 UI makeStyles 时默认导出与Router 做出反应 - How to Default export react withRouter while using material UI makeStyles 如何导出具有 2 个样式对象的 react material-ui 组件? (材质-ui V1) - How can I export a react material-ui component with 2 styles object? (material-ui V1) 如何在反应中更改 material-ui 文本字段标签样式 - How to change material-ui Textfield label styles in react material-ui(React)中如何覆盖(overwrite)类和styles - How to override (overwrite) classes and styles in material-ui (React) React:如何组合 Material-UI 中标记的多个样式 - React: How to combine each multiple styles marked in Material-UI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM