无法在 styled-component 中扩展 React 组件

huangapple go评论106阅读模式
英文:

Unable to extend react component in styled-component

问题

我正在尝试在 styled-component 中扩展 React 组件并尝试在扩展的组件上添加自定义样式,但无法看到我所应用的样式更改。

我已经在 /src/newbutton.js 中创建了一个按钮组件,以下是代码示例:

  1. import styled from "styled-components";
  2. const Button = styled.button`
  3. background: ${props => props.primary ? "palevioletred" : "white"};
  4. color: ${props => props.primary ? "white" : "palevioletred"};
  5. font-size: 1em;
  6. margin: 1em;
  7. padding: 0.25em 1em;
  8. border: 2px solid palevioletred;
  9. border-radius: 3px;
  10. `;
  11. export const NewButton = ({ className, children }) => {
  12. return (
  13. <Button primary>Primary</Button>
  14. )
  15. }

然后,在 /src/custom-button.js 中扩展并创建另一个带有自定义样式的按钮组件,以下是代码示例:

  1. import styled from "styled-components";
  2. import { NewButton } from './button';
  3. const ButtonWrapper = styled(NewButton)`
  4. width: 100%;
  5. color: red;
  6. `;
  7. export const ExtendedButton = ({ className, children }) => {
  8. return (
  9. <ButtonWrapper />
  10. )
  11. }

我已经添加了自定义样式,如 width: 100%color: red,但它不会应用于 ExtendedButton。实际上,colorwidthNewButton 相同。

英文:

I am trying to extent react component in styled-component and trying to add custom style on extended component but unable to see the style changes that I am applying

I have created a button component in /src/newbutton.js with following code

  1. import styled from &quot;styled-components&quot;;
  2. const Button = styled.button`
  3. background: ${props =&gt; props.primary ? &quot;palevioletred&quot; : &quot;white&quot;};
  4. color: ${props =&gt; props.primary ? &quot;white&quot; : &quot;palevioletred&quot;};
  5. font-size: 1em;
  6. margin: 1em;
  7. padding: 0.25em 1em;
  8. border: 2px solid palevioletred;
  9. border-radius: 3px;
  10. `;
  11. export const NewButton = ({ className, children }) =&gt; {
  12. return (
  13. &lt;Button primary&gt;Primary&lt;/Button&gt;
  14. )
  15. }

And extending and creating another button component with custom style in /src/custom-button.js with following code

  1. import styled from &quot;styled-components&quot;;
  2. import { NewButton } from &#39;./button&#39;
  3. const ButtonWrapper = styled(NewButton)`
  4. width: 100%;
  5. color: red
  6. `;
  7. const ExtendedButton = ({ className, children }) =&gt; {
  8. return (
  9. &lt;ButtonWrapper /&gt;
  10. )
  11. }

I have added the custom style like width: 100% & color: red but it is not applying on ExtendedButton. Infect colour and width is same as NewButton

答案1

得分: 1

你需要在你的NewButton中传递一个className来进行定制,使用styled-components

Styled components通过创建与组件及其CSS相关联的唯一className来工作。

  1. export const NewButton = ({ className, children }) => {
  2. return (
  3. <Button className={className} primary>Primary</Button>
  4. )
  5. }
英文:

You need to pass a className to your NewButton in order to customize it, using styled-components.

Styled components works by creating a unique className that associated with a component and its CSS.

  1. export const NewButton = ({ className, children }) =&gt; {
  2. return (
  3. &lt;Button className={className} primary&gt;Primary&lt;/Button&gt;
  4. )
  5. }

答案2

得分: 0

  1. 我正在发布完整的工作代码以供将来参考,基于 @Flat Globe 的解决方案。它按预期运行良好。
  2. 我已经通过在 `/src/newbutton.js` 中添加 `className` 修改了按钮组件代码,代码如下:
  3. ```jsx
  4. import styled from "styled-components";
  5. const Button = styled.button`
  6. background: ${props => props.primary ? "palevioletred" : "white"};
  7. color: ${props => props.primary ? "white" : "palevioletred"};
  8. font-size: 1em;
  9. margin: 1em;
  10. padding: 0.25em 1em;
  11. border: 2px solid palevioletred;
  12. border-radius: 3px;
  13. `;
  14. export const NewButton = ({ className, children }) => {
  15. return (
  16. <Button primary className={className}>Primary</Button>
  17. )
  18. }

我也通过在 /src/custom-button.js 中传递 className 修改了扩展按钮的代码。请查看下面的完整代码:

  1. import styled from "styled-components";
  2. import { NewButton } from './button'
  3. const ButtonWrapper = styled(NewButton)`
  4. width: 100%;
  5. color: red
  6. `;
  7. const ExtendedButton = ({ className, children }) => {
  8. return (
  9. <ButtonWrapper className="extended-button"/>
  10. )
  11. }
  1. <details>
  2. <summary>英文:</summary>
  3. I am posting the complete working code for future reference based on @Flat Globe solution. And it is working fine as expected.
  4. I have modified the Button component code just by adding `className` in `/src/newbutton.js` with following code

import styled from "styled-components";

const Button = styled.button`
background: ${props => props.primary ? "palevioletred" : "white"};
color: ${props => props.primary ? "white" : "palevioletred"};

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

export const NewButton = ({ className, children }) => {
return (
<Button primary className={className}>Primary</Button>
)
}

  1. I have also modified the extended-button code by just passing the `className` in `/src/custom-button.js`. check the full code below

import styled from "styled-components";
import { NewButton } from './button'

const ButtonWrapper = styled(NewButton)
width: 100%;
color: red
;

const ExtendedButton = ({ className, children }) => {
return (
<ButtonWrapper className="extended-button"/>
)
}

  1. </details>

huangapple
  • 本文由 发表于 2023年2月19日 01:47:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定