简体   繁体   English

以嵌套方式使用material-ui v1的withStyles

[英]Using withStyles of material-ui v1 in a nested manner

So I have this component that I am trying to write in react manner. 因此,我有这个组件正在尝试以React方式编写。 Where each of the major containers are separated as React class component. 每个主要容器都作为React类组件分开的地方。 Now I know how to use withStyles with one component : export default withStyles(styles)(MyComponent); 现在,我知道如何将withStyles与一个组件一起使用:导出默认withStyles(styles)(MyComponent);

But when you have more than two components, how do you use withStyles. 但是,当您拥有两个以上的组件时,如何使用withStyles。 Here's the code : 这是代码:

    class AtmanPage extends Component {
  render() {
    return (
      <AtmanAppBar />
    );
  }
}


class AtmanAppBar extends Component {
  render() {
    return (
      <div className= {this.props.classes.root}>
      <AppBar position="static">
        <Toolbar>
          <IconButton className= {this.props.classes.menubutton} color="contrast" aria-label="Menu">
            <MenuIcon />
          </IconButton>
          <Typography type="title" color="inherit" className={this.props.classes.flex}>
            Title
          </Typography>
          <Button color="contrast">Login</Button>
        </Toolbar>
      </AppBar>
          </div>
    );
  }
}

const styles = theme => ({
  root: {
    marginTop: theme.spacing.unit * 3,
    width: '100%',
  },
  flex: {
    flex: 1,
  },
  menuButton: {
    marginLeft: -12,
    marginRight: 20,
  },
});

export default withStyles(styles) AtmanPage ?;

Now the question mark is about what else should be done to pass the styles as a prop to AtmanAppBar through AtmanPage. 现在,问号是有关通过AtmanPage将样式作为道具传递给AtmanAppBar的其他措施。

Your issue is not related to withStyles but in the way you organize your components and exports. 您的问题与withStyles而与组织组件和导出的方式有关。

Solution 1: 解决方案1:

You can put each component in a separate file and use 您可以将每个组件放在单独的文件中并使用

export default withStyles(styles)(MyComponent);

in each file as you have mentioned in your post. 就像您在帖子中提到的那样。

Solution 2: 解决方案2:

You can do a simple export for both of your classes if you wish to keep them in the same file: 如果希望将两个类保存在同一文件中,则可以对两个类进行简单导出:

export const StyledAtmanAppBar = withStyles(styles)(AtmanAppBar);
export const StylesAtmanPage = withStyles(styles)(AtmanPage);

Then you need to import them like so in another file: 然后,您需要像这样在另一个文件中导入它们:

import {StyledAtmanAppBar, StylesAtmanPage} from './path/to/your/file';

Looking to handle this shizzle myself I came across a helpful util to use Styled-Components™-like API via Material-UI. 为了自己解决这个麻烦,我遇到了一个有用的工具,可以通过Material-UI使用类似于Styled-Components™的API。

Gist example 要点示例

After importing it as: 导入为:

import styled from '../utils/styled';

you can create many Styled-Components-lookalikes in one file: 您可以在一个文件中创建许多样式化组件外观:

const List = styled('ul')(theme => ({
  padding: theme.spacing.unit,
  margin: 0,
}));

One difference with default withStyles() API is, that this util applies the styles to the root class, so you cannot nest many styles styles for one component (probably could be easily upgraded to allow that tbh). 与默认withStyles() API的不同之处在于,该withStyles()将样式应用于root类,因此您不能为一个组件嵌套许多样式样式(可能很容易升级以允许该tbh)。

But it's really handy for simple styles re-usability without the need to create too many files. 但这对于简单样式的可重用性非常方便,无需创建太多文件。

Credit to the (probable) author 归功于(可能的)作者

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

相关问题 如何使用 Typescript 实现 Material-UI ThemeProvider 和 WithStyles - How to implement Material-UI ThemeProvider and WithStyles using Typescript 使用 styed-components 和 Material-UI withStyles 的 TextField 样式 - TextField Style using styed-components and Material-UI withStyles 默认将material-ui v1按钮设置为白色 - Make material-ui v1 Button white by default 如何导出具有 2 个样式对象的 react material-ui 组件? (材质-ui V1) - How can I export a react material-ui component with 2 styles object? (material-ui V1) 在 React with Jest 中为 @material-ui withStyles 创建手动模拟 - Create manual mock for @material-ui withStyles in React with Jest 外部文件上的材质-ui withStyles无法正常工作 - React material-ui withStyles on external file not working “ material-ui”不包含名为“ withStyles”的导出 - 'material-ui' does not contain an export named 'withStyles' Material-UI v1抽屉组件在iOS 13 beta版中不起作用 - Material-UI v1 Drawer component isn't working in the iOS 13 beta build 带有Redux的Material UI v1 - 如何导出 - Material UI v1 with Redux - How to export Material-UI:WithStyles(ForwardRef(Dialog)) 中未实现提供给 classes 道具的关键 `paperFullWidth` - Material-UI: The key `paperFullWidth` provided to the classes prop is not implemented in WithStyles(ForwardRef(Dialog))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM