简体   繁体   English

Material UI如何使用整个组件的一种主要通用样式设置子组件的外部样式

[英]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: classNameRed.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.jsDemo.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.

相关问题 反应在其中一个子组件中生成图像时如何在整个屏幕顶部显示图像 - React how to display an image on top of the entire screen when the image is generated in one of the sub-components 如何正确地将一个组件的代码拆分为多个子组件? - How to properly split the code of a component into sub-components? Angular 2,不渲染动态组件的子组件 - Angular 2, sub-components of dynamic component are not rendered 使用动态子组件对组件进行反应 - React Component with dynamic Sub-Components 将material-ui生成的样式放置在子组件的样式之后,再放置父组件的样式? - Place styles generated by material-ui for children components after style generated for parent component? 在使用Jest单元测试React组件时如何模拟子组件 - How to mock out sub-components when unit testing a React component with Jest 当React / Flux中有多个小的可重复子组件时,如何管理组件渲染 - How to manage component rendering when there are several small, repeatable sub-components in React/Flux 自定义 React 组件样式被 Material-UI 样式覆盖 - Custom React Component styles being overwritten by Material-UI style 如何让react-table的第一个子组件默认打开并手动打开其他子组件 - How to make first sub-component of react-table open by default and open other sub-components manually 棱角分明。 如何在外部定义点击,不包括子组件? - Angular. How to define a click outside, excluding sub-components?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM