我有一个问题:React样式组件&(parent Selector)
目标:我想为容器的儿童组件(标题,登录名,页脚)提供弹性样式。
给定以下代码(以CREATE-REACT-APP -TYPESCRIPT开始):
src/app.tsx
import React from 'react';
import logo from './logo.svg';
import styled from 'styled-components';
import Footer from './component/Footer/Footer';
import Login from './component/Login/Login';
import Header from './component/Header/Header';
function App() {
return (
<>
<Container>
<Header />
<Login />
<Footer />
</Container>
</>
);
}
const Container = styled.div`
display: flex;
flex-direction: column;
height: 100vh;
background-color: #7f8c8d;
&:nth-child(1) {
flex: 1 1 15%;
}
&:nth-child(2) {
flex: 1 1 75%;
}
&:nth-child(3) {
flex: 1 1 5%;
}
`;
export default App;
src/component/footer/footer.tsx
import React from 'react';
import { FC } from 'react';
import styled from 'styled-components';
interface FooterProps {}
const Footer: FC<FooterProps> = () => {
return <Container>Footer</Container>;
};
const Container = styled.footer`
display: flex;
justify-content: center;
background-color: #2980b9;
`;
export default Footer;
src/component/component/header/header/header.tsx
import React from 'react';
import { FC } from 'react';
import styled from 'styled-components';
interface HeaderProps {}
const Header: FC<HeaderProps> = () => {
return <Container>Header</Container>;
};
const Container = styled.header`
display: flex;
align-items: center;
justify-content: center;
background-color: #2980b9;
`;
export default Header;
src/components/login/login/login.tsx
import React from 'react';
import { FC } from 'react';
import styled from 'styled-components';
interface LoginProps {}
const Login: FC<LoginProps> = () => {
return <Container>Login</Container>;
};
const Container = styled.main`
display: flex;
justify-content: center;
background-color: #ecf0f1;
`;
export default Login;
应用程序渲染结果
问题部分是App.tsx(styled.div)的容器,
const Container = styled.div`
display: flex;
flex-direction: column;
height: 100vh;
background-color: #7f8c8d;
&:nth-child(1) {
flex: 1 1 15%;
}
&:nth-child(2) {
flex: 1 1 75%;
}
&:nth-child(3) {
flex: 1 1 5%;
}
`;
我尝试了几个代码,例如:(起作用)
const Container = styled.div`
display: flex;
flex-direction: column;
height: 100vh;
background-color: #7f8c8d;
& :nth-child(1) {
flex: 1 1 15%;
}
& :nth-child(2) {
flex: 1 1 75%;
}
& :nth-child(3) {
flex: 1 1 5%;
}
`;
和
const Container = styled.div`
display: flex;
flex-direction: column;
height: 100vh;
background-color: #7f8c8d;
& > :nth-child(1) {
flex: 1 1 15%;
}
& > :nth-child(2) {
flex: 1 1 75%;
}
& > :nth-child(3) {
flex: 1 1 5%;
}
`;
代码结果
我试图尝试的代码变化效果很好,但我不明白为什么它会这样起作用。
为什么这不起作用?
&:nth-child(1) {
flex: 1 1 15%;
}
&:nth-child(2) {
flex: 1 1 75%;
}
&:nth-child(3) {
flex: 1 1 5%;
}
Goal: I want to give flex style to the child components(Header, Login, Footer) of the Container.
Given the following code(started with create-react-app --typescript):
src/App.tsx
import React from 'react';
import logo from './logo.svg';
import styled from 'styled-components';
import Footer from './component/Footer/Footer';
import Login from './component/Login/Login';
import Header from './component/Header/Header';
function App() {
return (
<>
<Container>
<Header />
<Login />
<Footer />
</Container>
</>
);
}
const Container = styled.div`
display: flex;
flex-direction: column;
height: 100vh;
background-color: #7f8c8d;
&:nth-child(1) {
flex: 1 1 15%;
}
&:nth-child(2) {
flex: 1 1 75%;
}
&:nth-child(3) {
flex: 1 1 5%;
}
`;
export default App;
src/component/Footer/Footer.tsx
import React from 'react';
import { FC } from 'react';
import styled from 'styled-components';
interface FooterProps {}
const Footer: FC<FooterProps> = () => {
return <Container>Footer</Container>;
};
const Container = styled.footer`
display: flex;
justify-content: center;
background-color: #2980b9;
`;
export default Footer;
src/component/Header/Header.tsx
import React from 'react';
import { FC } from 'react';
import styled from 'styled-components';
interface HeaderProps {}
const Header: FC<HeaderProps> = () => {
return <Container>Header</Container>;
};
const Container = styled.header`
display: flex;
align-items: center;
justify-content: center;
background-color: #2980b9;
`;
export default Header;
src/component/Login/Login.tsx
import React from 'react';
import { FC } from 'react';
import styled from 'styled-components';
interface LoginProps {}
const Login: FC<LoginProps> = () => {
return <Container>Login</Container>;
};
const Container = styled.main`
display: flex;
justify-content: center;
background-color: #ecf0f1;
`;
export default Login;
App rendering result
the problem part is Container at App.tsx (styled.div)
const Container = styled.div`
display: flex;
flex-direction: column;
height: 100vh;
background-color: #7f8c8d;
&:nth-child(1) {
flex: 1 1 15%;
}
&:nth-child(2) {
flex: 1 1 75%;
}
&:nth-child(3) {
flex: 1 1 5%;
}
`;
I tried several codes like: (it works)
const Container = styled.div`
display: flex;
flex-direction: column;
height: 100vh;
background-color: #7f8c8d;
& :nth-child(1) {
flex: 1 1 15%;
}
& :nth-child(2) {
flex: 1 1 75%;
}
& :nth-child(3) {
flex: 1 1 5%;
}
`;
and
const Container = styled.div`
display: flex;
flex-direction: column;
height: 100vh;
background-color: #7f8c8d;
& > :nth-child(1) {
flex: 1 1 15%;
}
& > :nth-child(2) {
flex: 1 1 75%;
}
& > :nth-child(3) {
flex: 1 1 5%;
}
`;
code result
The code I tried to change worked well, but I don't understand why it works like this.
Why isn't this working?
&:nth-child(1) {
flex: 1 1 15%;
}
&:nth-child(2) {
flex: 1 1 75%;
}
&:nth-child(3) {
flex: 1 1 5%;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论