reactjs - ReactJs StyledComponent 和 ClassName
问题描述
你好我有一个问题我相信我的代码对于简单的事情来说太大了
基本上我有一个网站的包装:
export const Wrapper = styled.div`
min-height:100vh !important;
`;
我的标题现在是这样的:
<Styled.WrapHeader>
<DefaultContainer>
<Styled.NavStickey />
</DefaultContainer>
</Styled.WrapHeader>
我使用了 3 个 div 来制作一个简单的 100% 宽度的 div 和一个导航栏为 css 的容器:
const WrapHeader = styled.div`
background: #1269DB;
height: 130px;
width:100%;
`;
const NavStickey = styled.div`
width:100%;
height: 60px;
background: red;
`;
在这里我有我的容器:
export const DefaultContainer = styled.div`
display: flex;
flex-direction: row;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: justify;
justify-content: space-between;
background: transparent !important;
`
我认为非常大我想为一个类分离容器的边距和填充,并在 div 中调用 className 我将如何在样式中得到它?
像这样:
.divct{
display: flex;
flex-direction: row;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: justify;
justify-content: space-between;
background: transparent !important;
}
.container {
max-width: 1000px;
padding: 0px 30px;
margin: 0px auto;
}
div className = "divct container"
解决方案
您可以像这样创建 CSS 模板的片段:
import styled, { css } from 'styled-components'
// Not a styled component, just a css template.
const DivContainer = css`
display: flex;
flex-direction: row;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: justify;
justify-content: space-between;
background: transparent !important;
`
const Container = css`
max-width: 1000px;
padding: 0px 30px;
margin: 0px auto;
`
然后,您可以将它们组合成一个样式化的组件,就像在常规组件中加入两个类名一样:
const WrapHeader = styled.div`
background: #1269DB;
height: 130px;
width:100%;
${DivContainer}
${Container}
`
这就像任何其他模板字符串一样工作(因为它们就是这样)。所以结果将合并所有样式,如下所示:
// This is what the result will look like internally.
const WrapHeader = styled.div`
background: #1269DB;
height: 130px;
width:100%;
display: flex;
flex-direction: row;
-webkit-box-align: center;
align-items: center;
-webkit-box-pack: justify;
justify-content: space-between;
background: transparent !important;
max-width: 1000px;
padding: 0px 30px;
margin: 0px auto;
`
推荐阅读
- elixir - Ecto.Repo.update_all 用于原子更新?
- git-bash - Git Bash如何在终端执行命令后删除空行
- r - 如何使用 R、stringr 等动态删除或替换字符串中具有不确定数字的单词?
- php - 如何在获取时从获取mysql中乘以值
- asp.net - 页面索引在第二页以后不起作用
- mysql - 将手动分配的 ID 重置为自动递增
- c# - Webdriver Driver Mananger 2.7.0 没有从 Nuget 与 .Net 框架 4.5 安装
- laravel - Laravel 5.8 创建递归集合
- haskell - Haskell 中的布尔非穷举模式
- alexa - 在 Alexa 中更改 SessionEndedRequest 处理程序响应