【问题标题】:How do I pass a prop from a Parent styled component to its children's styled components no matter how deep无论多深,如何将道具从父样式组件传递到其子样式组件
【发布时间】:2021-07-12 11:09:21
【问题描述】:

我有这些样式组件:

import styled, { css } from 'styled-components'

const Container = styled.div`
  background-color: ${props => props.theme === "light" ? "hsl(0, 0%, 98%)" : "hsl(235, 24%, 19%)" };
  border-radius: 4px;
  box-shadow: ${props => props.theme === "light" ? "0px 35px 50px -15px rgba(194, 195, 214, 0.5)" : "0px 35px 50px -15px rgba(0, 0, 0, 0.5)" };
`

const Footer = styled.footer`
  padding: 25px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: ${props => props.theme === "light" ? "hsl(236, 9%, 61%)" : "hsl(234, 11%, 52%)" };
  font-size: 14px;
`

const Nav = styled.nav`
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: inherit;
`

const NavItem = styled.a`
  cursor: pointer;
  display: inline-block;
  color: inherit;

  &:hover {
    color: ${props => props.theme === "light" ? "hsl(235, 19%, 35%)" : "hsl(236, 33%, 92%)" };
  }

  &:not(:last-of-type) {
    margin-right: 15px;
  }

  ${({ active }) =>
    active &&
    css`
      color: hsl(220, 98%, 61%);
    `
  }
`

const ClearList = styled.p`
  color: inherit;
  cursor: pointer;

  &:hover {
    color: ${props => props.theme === "light" ? "hsl(235, 19%, 35%)" : "hsl(234, 39%, 85%)" };
  }
`

我有一个名为 TodoList 的组件,它带有一个 theme 道具,可以像这样传递给样式化的组件:

const TodoList = ({ theme }) => {
  return (
    <Container theme={theme}>
      <Footer theme={theme}>
        <p>5 items left</p>
        <Nav>
          <NavItem theme={theme}>All</NavItem>
          <NavItem theme={theme}>Active</NavItem>
          <NavItem theme={theme}>Completed</NavItem>
        </Nav>
        <ClearList theme={theme}>Clear Completed</ClearList>
      </Footer>
    </Container>
  )
}

export default TodoList

如何在 Container 元素中只调用一次 theme 属性,并使其可供所有子元素(例如 Clearlist、NavItems 和 Footer)访问,而无需显式传递它们

谢谢

【问题讨论】:

  • 你不需要。使用 styled-components Provider 并为它们定义主题,主题道具将在您的 styled.element`` 中可用。你的做法是多余的。
  • 我有一个类似的问题,有人在这里为我回答:长话短说,道具不会以这种方式级联,使用主题可以通过将 var 保持在全局来解决这个问题等级stackoverflow.com/questions/65887861/…

标签: javascript css reactjs styled-components react-props


【解决方案1】:

您可以使用 ThemeProvider 解决此问题

访问此链接https://styled-components.com/docs/advanced

例子

// Define our button, but with the use of props.theme this time
const Button = styled.button`
  color: ${props => props.theme.fg};
  border: 2px solid ${props => props.theme.fg};
  background: ${props => props.theme.bg};

  font-size: 1em;
  margin: 1em;
  padding: 0.25em 1em;
  border-radius: 3px;
`;

// Define our `fg` and `bg` on the theme
const theme = {
  fg: "palevioletred",
  bg: "white"
};

// This theme swaps `fg` and `bg`
const invertTheme = ({ fg, bg }) => ({
  fg: bg,
  bg: fg
});

render(
  <ThemeProvider theme={theme}>
    <div>
      <Button>Default Theme</Button>

      <ThemeProvider theme={invertTheme}>
        <Button>Inverted Theme</Button>
      </ThemeProvider>
    </div>
  </ThemeProvider>
);

【讨论】:

    猜你喜欢
    • 2022-11-15
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 2021-07-10
    • 2019-09-23
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    相关资源
    最近更新 更多