styled-components: parent styles has more priority then child

大兔子大兔子 提交于 2019-12-24 21:35:40

问题


image

export const LogOutButton = styled.button`
  display: inline-block;
  ....
`;
export default styled(ThemedApp)`
  ....
  button {
    ....
    display: flex;
    ....
  }
`;

as you see, Logout button(has class gBuhXv) has display: flex, instead of inline-block, because priority of parent(ThemedApp, .jCe...) is bigger

The rule that leads to this is p.column { text-align: right; } can be overwritten by body p.column { text-align: left; }, cause it more specific

Its right behavior, but not that I expect, how to make priority of Logout button bigger?


回答1:


The problem was that

const globalStyles = styled.div`
p {
  color: ${props => props.theme.somecolor};
}
a {
  color: ${props => props.theme.somecolor};
}`

isn't the right usage of styled-components.

Instead, I have to define components and extend them:

const P = styled.p`
  color: ${props => props.theme.somecolor};
`
const A = styled.A`
  color: ${props => props.theme.somecolor};
`
const CustomA = A.extend`
  color: mycolor;
`


来源:https://stackoverflow.com/questions/45610529/styled-components-parent-styles-has-more-priority-then-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!