简体   繁体   English

通过嵌套的样式组件传递道具

[英]Passing props through nested Styled Components

Take this simple situation:采取这个简单的情况:

I have a container, with an h1 nested inside of it.我有一个容器,里面嵌套了一个 h1。 I want to pass some props into both elements that will change their colour depending on a light / dark theme;我想将一些道具传递给这两个元素,这些道具将根据明暗主题改变它们的颜色; eg: white text / black bg ||例如:白色文字/黑色背景|| black text / white bg黑色文字/白色背景

Problem: it would appear the props (color & text) being passed to the styled components are not cascading in the way that I would assume they would.问题:看起来传递给样式化组件的道具(颜色和文本)并没有像我假设的那样级联。

Simple example:简单的例子:

export function Heading(props) {
return (
    <Container {...props}> // I would expect the props obj to be available by nested Heading el
      <Heading>
        {props.text}
      </Heading>
    </Container>
  )
}

// styled components 

export const Container = styled.div`
  padding: 20px;
  background-color: ${props => props.color === dark ? "black" : "white";
`;

export const Heading = styled.h1`
  background-color: ${props => props.color === dark ? "white" : "black"; // does not take affect
`;

Instead I have to do:相反,我必须这样做:

    <Container {...props}> // Pass to container
      <Heading {...props}> // Pass to heading el
        {props.text}
      </Heading>
    </Container>

Question :问题

Can I get the Heading to pick up the {...props} passed to Container without declaring it twice?我可以让Heading拿起传递给Container{...props}而不声明它两次吗?
Or am I misunderstanding how styled components are supposed to behave?还是我误解了样式化组件的行为方式?

Your example code is confusing because you have Heading inside of itself.您的示例代码令人困惑,因为您自己有Heading So let's call your function component MyHeading and the styled.h1 Heading .因此,让我们调用您的 function 组件MyHeadingstyled.h1 Heading

Props do not cascade from a parent to a child.道具不会从父母级联到孩子。 That's nothing to do with styled-components, that's just the way React works.这与样式化组件无关,这只是 React 的工作方式。 If you want to pass a prop to a child then you have to do it yourself like you have done in your second snippet.如果您想将道具传递给孩子,那么您必须像在第二个片段中所做的那样自己做。

That said, I don't think that passing down the props is the way to go in this particular situation.也就是说,在这种特殊情况下,我不认为传递道具是通往 go 的方式。 The light or dark theme is something that is global in nature, so rather than passing it down from component to component, we want to make the theme available through React contexts .浅色或深色主题本质上是全局性的,因此我们希望通过React 上下文使主题可用,而不是将其从组件传递到组件。 styled-components has some built-in support for this. styled-components 对此有一些内置支持 You can put the entire content of your app inside a ThemeProvider .您可以将应用程序的全部内容放在ThemeProvider中。 It makes it so that all styled components in the hierarchy beneath it will get access to a theme prop.它使得它下面的层次结构中的所有样式组件都可以访问theme道具。

so the best way to solve this is using themes .所以解决这个问题的最好方法是使用themes Since your container is a styled-component it has the built-in capability.由于您的容器是样式组件,因此它具有内置功能。

So if you add a theme prop to it, it will cascade the theme down to every child that is also a styled-component:所以如果你给它添加一个主题道具,它会将主题级联到每个子元素,这也是一个样式组件:

export function Heading(props) {
return (
    <Container theme={{mode: 'light'}}>
      <Heading> // this heading would receive this theme object as a prop.
        {props.text}
      </Heading>
    </Container>
  )
}

export const Heading = styled.h1`
  background-color: ${({theme}) => theme.mode === "dark" ? "white" : "black"};
`;

export const ButtonInsideHeading = styled.h1`
  background-color: ${({theme}) => theme.mode === "light" ? "black" : "white"};
`;

The same goes for that button you mentioned in the comment.您在评论中提到的那个按钮也是如此。 Styled-components will inject this theme prop, not matter how many levels deep down they are. Styled-components 将注入这个主题道具,无论它们有多少层次。 So your Button styled-component would inherit theme from the Heading that inherited itfrom Container .因此,您的 Button 样式组件将从Heading继承theme ,该主题从Container继承。 It can get a bit verbose, but not necessarily messy.它可能会有点冗长,但不一定是混乱的。

You can also use css variables as described in this post by Kent C.您还可以使用Kent C 在这篇文章中描述的 css 变量。 Dodds.多兹。 Works pretty well too, depending on your use case.效果也很好,具体取决于您的用例。

It is not possible, props in styled components should be passed directly to the component.这是不可能的,样式组件中的道具应该直接传递给组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM