[英]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。
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. 但这对于简单样式的可重用性非常方便,无需创建太多文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.