我在不同的组件之间使用了相似的布局,我希望能够重用创建布局的样式化组件。
const CoreContainer = styled.div`
display: flex;
xjustify-content: center;
flex-direction: column-reverse;
@media (min-width: 1025px) {
flex-direction: row;
}
height:100%;
`;
const FirstContainer = styled.div`
text-align:center;
background-color:#F8F8FA;
width:100%;
padding-top:4rem;
align-items: center;
justify-content: center;
display: flex;
@media (min-width: 1025px) {
xmin-width:700px;
width:68%;
}
`;
const SecondContainer = styled.div`
display:flex;
width:100%;
@media (min-width: 720px) {
-webkit-box-shadow: -1px 0 10px 0px rgba(0, 0, 0, 0.1);
box-shadow: -1px 0 10px 0px rgba(0, 0, 0, 0.1);
box-sizing: border-box;
display: flex;
justify-content: center;
}
@media (min-width: 1025px) {
width:33%;
}
`;
export class GeneralComponent extends Component{
render(){
if(this.props.value){
return <ComponentA />
}
else{
return <ComponentB />
}
}
}
//In a differnent file
export class ComponentA extends Component{
render(){
return(
<CoreContainer>
<FirstContainer>
{ someContentA}
</FirstContainer>
<SecondContainer>
{otherContentA}
</SecondContainer>
</CoreContainer>);
}
}
//In a differnent file
export class ComponentB extends Component{
render(){
return(
<CoreContainer>
<FirstContainer>
{ someContentB}
</FirstContainer>
<SecondContainer>
{otherContentB}
</SecondContainer>
</CoreContainer>);
}
}
我希望能够在不同的React组件中使用coreContainer,firstContainer和SecondContainer,有一种方法可以在不同组件之间重用样式化组件css,因此如果考虑到我更改布局的情况,它将在两个组件中都改变组件,我没有重复的代码?
谢谢。
答案 0 :(得分:2)
创建style.js
export const CoreContainer = styled.div`
display: flex;
justify-content: center;
flex-direction: column-reverse;
@media (min-width: 1025px) {
flex-direction: row;
}
height:100%;
`;
像这样导入两个文件:
import { CoreContainer } from 'path of the style.js';
答案 1 :(得分:0)
我认为您正在看这样的东西?
div.CoreContainer{
display: flex;
xjustify-content: center;
flex-direction: column-reverse;
@media (min-width: 1025px) {
flex-direction: row;
}
height:100%;
}
div.FirstContainer{
text-align:center;
background-color:#F8F8FA;
width:100%;
padding-top:4rem;
align-items: center;
justify-content: center;
display: flex;
@media (min-width: 1025px) {
xmin-width:700px;
width:68%;
}
}
div.SecondContainer{
display:flex;
width:100%;
@media (min-width: 720px) {
-webkit-box-shadow: -1px 0 10px 0px rgba(0, 0, 0, 0.1);
box-shadow: -1px 0 10px 0px rgba(0, 0, 0, 0.1);
box-sizing: border-box;
display: flex;
justify-content: center;
}
@media (min-width: 1025px) {
width:33%;
}
}