[英]Material UI How to set external styles for sub-components with one main common style for the entire component
I have a page made up of many sub-components. 我有一个由许多子组件组成的页面。 The page folder holds the page file, external styles file and each sub-components folders - which also consists of their own style. 页面文件夹包含页面文件,外部样式文件和每个子组件文件夹-它们也包含自己的样式。
I'm not sure how to set up an external shared common style file for all sub-components along with external styles for each of the sub-components. 我不确定如何为所有子组件设置外部共享的通用样式文件,以及为每个子组件设置外部样式。
Ie. IE浏览器。 DemoStyles.js
is the common styles and Demo.js
is where are the sub-components are called. DemoStyles.js
是常见样式, Demo.js
是调用子组件的位置。
Demo.js
: Demo.js
:
import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import styles from "./DemoStyles";
import Red from "./red/Red";
import Blue from "./blue/Blue";
function SharedStyles(props) {
return (
<React.Fragment>
<Red />
<Blue />
</React.Fragment>
);
}
SharedStyles.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(SharedStyles);
DemoStyles.js
: DemoStyles.js
:
export default theme => ({
title: {
color: "white",
fontSize: 30
}
});
The title
style is not being applied. title
样式未应用。
The className
is set in the Red.js
file: className
在Red.js
文件中设置:
TL;DR: I need one common external style file to apply to all subcomponents living in one folder; TL; DR:我需要一个公共的外部样式文件来应用于生活在一个文件夹中的所有子组件; and each subcomponent needs their own external style specific to it. 每个子组件都需要自己专用的外部样式。
Here is the code sample: https://codesandbox.io/s/rypoqk07lo 这是代码示例: https : //codesandbox.io/s/rypoqk07lo
SOLVED: I solved this by importing the demoStyles into the RedStyles.js file and then calling it with the spread operator and passing theme as an argument like so: 解决了:我通过将demoStyles导入RedStyles.js文件中,然后使用spread运算符调用它,并将主题作为参数传递来解决了这个问题,如下所示:
RedStyles.js
import demoStyles from "../DemoStyles";
export default theme => ({
...demoStyles(theme),
red: {
backgroundColor: "red"
}
});
code sample updated as well 代码示例也已更新
You forgot to pass the classes props on Demo.js
to your components like you do on <Red />
and <Blue />
. 您忘了像在<Red />
和<Blue />
上Demo.js
将Demo.js
上的类props Demo.js
给组件。
const { classes } = props;
return (
<React.Fragment>
<Red classes={classes} />
<Blue classes={classes} />
</React.Fragment>
);
Its good to remember that Material-UI has themes support . 谨记Material-UI具有主题支持 。 It's better to use it on Demo.js
depending of what you trying to do. 最好根据您要执行的操作在Demo.js
上使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.