简体   繁体   English

将material-ui生成的样式放置在子组件的样式之后,再放置父组件的样式?

[英]Place styles generated by material-ui for children components after style generated for parent component?

I am using material-ui which in turn uses JSS to style a website. 我正在使用material-ui ,后者又使用JSS来设计网站样式。

I have a component called Layout that sets margin of all its children (using all children selector & > * in its style). 我有一个名为Layout的组件,用于设置其所有子项的边距(使用其样式中的所有子项选择器& > * )。 That works but I also want the children to be able to override that margin in their own styles named in className property. 那行得通,但我也希望孩子们能够以在className属性中命名的自己的样式覆盖该边距。

This issue is caused by the fact that material-ui's withStyle function places the parent's style after children's styles inside html <head> . 此问题是由于material-ui的withStyle函数在html <head>内将父代样式放在子代样式之后 I could increase all chidren's style priority by doing something like withStyles(classes, { index: 1 })(ChildComponent) but that would be tedious and error prone. 我可以通过执行withStyles(classes, { index: 1 })(ChildComponent)来增加所有孩子的样式优先级,但这会很乏味并且容易出错。

What can I do to allow overriding parent defined style by children? 我该怎么做才能允许孩子覆盖父级定义的样式?

Also see this request . 另请参阅此请求

This is more of a workaround than a solution... 这不是解决方案,而是更多解决方法...

Its possible to have the child component's styles injected below the parent's if you use props.children in the parent component. 如果在父组件中使用props.children ,则可以将子组件的样式注入到父组件的下方。

See JSS injection order 请参阅JSS注入顺序

// If child components are imported after Layout,
// their styles will be injected below Layout's styles.
import Layout from "./Layout";
import ChildOne from "./ItemOne";
import ChildTwo from "./ItemTwo";

function App() {
  return (
    <Layout>
      <ChildOne />
      <ChildTwo />
    </Layout>
  );
}

编辑堆栈溢出:覆盖在父组件材质UI中设置的样式

Caveats 注意事项

Im not 100% sure that this behavior is guaranteed. 我不能100%保证可以保证这种行为。

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

相关问题 自定义 React 组件样式被 Material-UI 样式覆盖 - Custom React Component styles being overwritten by Material-UI style 使用 material-ui 更改父组件中的子组件样式 - Changing child-component styles in parent with material-ui 样式被 Material-UI 样式覆盖 - Styles being overwritten by Material-UI style 材料的UI <Grid item> 组件的宽度为100%的子代与父代重叠 - material-ui <Grid item> component's children with 100% width overlaps parent Material-UI 样式:将功能组件转换为类组件 - Material-UI styles: convert functional component to class component 如何导出具有 2 个样式对象的 react material-ui 组件? (材质-ui V1) - How can I export a react material-ui component with 2 styles object? (material-ui V1) Material UI如何使用整个组件的一种主要通用样式设置子组件的外部样式 - Material UI How to set external styles for sub-components with one main common style for the entire component Material UI 样式在组件中不起作用(警告:`@material-ui/styles` 的几个实例) - Material UI styles not working in component (warning: several instances of `@material-ui/styles`) 使用 styed-components 和 Material-UI withStyles 的 TextField 样式 - TextField Style using styed-components and Material-UI withStyles Material-UI Select 组件与自定义子项 - Material-UI Select component with custom children item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM