css - 如何在一个 react ​​组件中设置另一个组件的样式?

标签 css reactjs styles

我是 css 的新手,我不知道如何在 React 中将一个组件放置在另一个组件中。我可以单独展示它们,但是当我把一个放在另一个里面时。我没有看到里面的那个。我认为问题出在 css 文件中

#homePage{
   section{
      h1{
        text-align: left; //this is shown
      }
      //here I want to add the other React component but I don't know how

   }
}

渲染方法:

<div id="homePage">
  <Component1>
    <section>
     <h1>Hi</h1>
     <Component2>
     </Component2>
    </section>
  </Component1>
</div>

谢谢。

最佳答案

据我了解,您可以在 Component2 的 HTML 标记中定义 className 属性。

class Component2 extends Component{ 

render(){

    return(
        <section className="component2styles">
            This is Component2
        </section >
    );
} }

现在,您可以将您的样式表更改为

#homePage{
   section{
      h1{
        text-align: left; //this is shown
      }
      //components2 style will be nested here

     section.component2styles{
       border:1px solid blue;
     }

   }
}

或者作为替代方案,您可以尝试内联样式,它似乎在 React 开发中获得了很大的吸引力。

render(){
 var styleobj={color:'red'};
 return( <section style={styleobj} > This is Component 2 </section> ) 
}

关于css - 如何在一个 react ​​组件中设置另一个组件的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37671712/

相关文章:

javascript - react 按钮事件样式

reactjs - 函数内部的 Jest 模拟函数

button - ExtJS按钮样式工具栏

css - div 边框折叠在 bootstrap 中不起作用

css - 我应该避免在 CSS 中使用 !important 吗?

html - 菜单字体大小不一样

html - css关键帧位置问题

node.js - crypto.getCurves 未定义

WPF 绑定(bind)到数据触发器中的父内容

html - 如何对齐选项卡旁边的图像