【问题标题】:Styling child components dynamically in react在反应中动态设置子组件的样式
【发布时间】:2020-01-25 13:41:09
【问题描述】:

我正在使用 sass 通过为每个组件导入已编译的 css 来设置组件的样式,这不会很好地动态设置样式或缩放

上下文:

我有一个主应用程序组件的主样式表,其中包含具有媒体查询和不同自定义列大小的响应式 UI 类。我不想在主组件的每个子组件中重复这些类...只是为了确保每个都可以访问它们...我是否以错误的方式处理组件样式?

我如何让我的子组件继承 css 类?我必须将它们作为道具传递吗?我觉得有更好的方法来解决这个问题......

动态更改全局样式状态时如何保持全局样式状态?

【问题讨论】:

    标签: css reactjs sass


    【解决方案1】:

    我正在使用 sass 通过导入已编译的 css 来设置我的组件的样式 每个组件

    我不推荐这种方法。 React 是用 Webpack 编译的。如果您使用 create-react-app 来初始化您的新项目,您只需将任何 .scss 文件导入到您的 index.js 中,Webpack 会将其编译为 .css 文件并在构建时将其注入到您的 HTML 中。如果你这样做,你可以在你的 React 应用程序的任何地方使用任何 css 类。

    更多信息在这里:https://scotch.io/tutorials/using-sass-in-create-react-app-v2

    在这里:https://create-react-app.dev/docs/adding-a-sass-stylesheet

    P.s.:我建议您在 src 中创建一个 scss 文件夹,其中包含一个主 scss 文件(例如 app.scss)。在 app.scss@import 你所有其他可以继承变量的 scss 文件等等。然后将 app.scss 导入到你的 index.js

    【讨论】:

    • 我遇到的下一个问题是我需要动态更改 css 值,例如颜色主题。我相信这意味着我必须在某些情况下通过传递道具手动进行样式设置......这听起来很乱
    • 那么你应该使用 CSS-in-JS 库。我认为最好的是emotion.js。还有其他类似styled-components。您可以在此处找到完整的比较列表:https://github.com/tuchk4/awesome-css-in-js
    • 我可以混合使用吗?还是我应该完全重构我的 sass 以适应 CSS-in-JS 库?
    • mixin 应该都不是问题。例如,您可以在 scss 文件中设置所有布局和网格系统的样式,然后在 CSS-in-JS 中创建自定义按钮,根据主题更改颜色。
    • 另一种解决方案(完全在 scss 中)是根据主题在您的主体上添加一个类,并根据该类创建每个组件的不同版本。 This is a good post to understand the concept
    【解决方案2】:

    我找到了一个对我有用的解决方案,正如 Basile 所建议的那样,我正在使用情感 css-in-js 库,其中样式进入样式化组件。 Emotion 带有一个主题机制来动态设置 CSS 样式,并保持全局样式。

    css-in-js 样式的组件

    const MButton = styled.button`
      background-color: ${(props) => props.theme.colors.primarycolor};//using theme
      border: 3px solid $color;
      border-radius: 20px 20px 20px 20px;
      -moz-border-radius: 20px 20px 20px 20px;
      -webkit-border-radius: 20px 20px 20px 20px;
      -ms-border-radius: 20px 20px 20px 20px;
      -o-border-radius: 20px 20px 20px 20px;
    `;
    export default class index extends Component { ... your component
    

    对于全局样式和主题,我使用了情绪主题、主题提供程序和全局组件。我在一个单独的文件中定义我的主题,导入它,应用

    const makeGlobalStyles = (theme) => css` any additional global styling not dependent on differing themes in theme`; 
    //supply styles to global component which manages global theme style state
    const GlobalStyles = withTheme(({ theme }) => (
      <Global styles={makeGlobalStyles(theme)} />
    ));
    

    将主题存储为应用程序主组件中的状态...

    this.state = {
          viewState: "DARK",
          theme: theme.DARK,//use a method to change these states to then change the theme styles used across the app
    

    然后向所有使用它的组件实时提供主题状态

    render(){
    const {viewState,theme} = this.state;
    return(
    <ThemeProvider theme={theme}>
       <GlobalStyles />
         any logic for your application, pass prop theme to components to dynamically style to the theme
         <SomeComponent theme={theme}/>
    <ThemeProvider/>
    )}
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多