扩展样式组件属性?

Extending styled components properties?

H1.js

export default styled.h1`
  margin: 0px;
  color: white;
`;

我想改变这个组件的颜色,我试过了

import H1 from "./H1";

const ColoredH1 = styled(H1)`
    color: "black"
`; 

但这不是改变H1的颜色吗?

不要将 "black" 与引号一起使用 ,请记住您在 styled-component 中写 CSS,因此 "black"不是有效的颜色,尽管 black 可以。

const ColoredH1 = styled(H1)`
  /* color: "black"; */  /* Invalid Color */

  color: black;          /* Valid Color */
  color: ${"black"}      /* Or use a valid color representation as String */
`;

color: black代替color: "black"

import H1 from "./H1";

const ColoredH1 = styled(H1)`
    color: black;
`; 

供您理解

const Button = styled.button`
  color: red;
  font-size: 1em;
  margin: 1em;
  padding: 0.25em 1em;
  border: 2px solid red;
  border-radius: 3px;
`;

const CoralButton = styled(Button)`
  color: coral;
  border-color: coral;
`;
render(
  <div>
    <Button>Normal Button</Button>
    <CoralButton>Tomato Button</CoralButton>
  </div>
);