ReactJS Material UI withStyles包括TS中的主题

时间:2018-11-07 22:39:05

标签: reactjs typescript material-ui

我试图将Material UI仪表板转换为TypeScript。 https://github.com/mui-org/material-ui/blob/master/docs/src/pages/page-layout-examples/dashboard/Dashboard.js

当前,我面临的问题是在导出仪表板时,无法在withStyles函数上设置CSS样式定义。

const styles = (theme: Theme) => ({
  root: {
    display: 'flex',
  },
  toolbar: {
    paddingRight: 24, // keep right padding when drawer closed
  },
  toolbarIcon: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'flex-end',
    padding: '0 8px',
    ...theme.mixins.toolbar,
  },
  appBar: {
    zIndex: theme.zIndex.drawer + 1,
    transition: theme.transitions.create(['width', 'margin'], {
      easing: theme.transitions.easing.sharp,
      duration: theme.transitions.duration.leavingScreen,
    }),
  },
  .....
});

如果我正确理解,则需要此主题参考以将样式调整为当前主题。但是,调用样式函数时如何获得当前主题?

export default withStyles(styles(/*WHAT TO PUT IN HERE?*/))(Dashboard);

1 个答案:

答案 0 :(得分:1)

export default withStyles(styles)(Dashboard);

withStyles将确定是否需要使用主题进行调用。

withThemewithStyles(stylesObjectOrCreator, { withTheme: true })仅在需要通过道具访问组件内部的theme时才需要。

API:https://material-ui.com/customization/css-in-js/#withstyles-styles---options----higher-order-component