我有一个问题:React样式组件&(parent Selector)

发布于 2025-02-04 09:28:47 字数 3715 浏览 2 评论 0原文

目标:我想为容器的儿童组件(标题,登录名,页脚)提供弹性样式。

给定以下代码(以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

enter image description here

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

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文