我正在将 React 与 Material UI 结合使用,并且现在正在重构我的一个组件。我的原始组件如下所示:
// All the import statements here
const styles = theme => ({
container: {
margin: 'auto',
maxWidth: '1200px',
background: theme.palette.primary.main
// More styles here
},
});
const Component = ({ classes }) => (
<div>
<div className={classes.container}>
{/* Some content */}
</div>
<div className={classes.container}>
{/* Some content */}
</div>
</div>
);
export default withStyles(styles)(Component);
Run Code Online (Sandbox Code Playgroud)
但是在重构组件之后,我需要访问两个不同组件中的“容器”类。我能想到的唯一方法是在我的根目录中创建一个名为 的新文件sharedStyles.js,然后我将其导入这两个组件并像这样扩展其样式对象:
文件:sharedStyles.js
export default theme => ({
container: {
margin: 'auto',
maxWidth: '1200px',
background: theme.palette.primary.main
// Some …Run Code Online (Sandbox Code Playgroud)