Styled Components 渲染基于 props 的 Styled 标签

我正在构建一个应用程序并希望拥有主要、次要和三次按钮,我不想创建 3 个单独的组件,这些组件是相同的但具有不同的样式组件,因此我想有条件地显示正确的标签,如下面的代码所示,但我收到错误 JSX 元素类型“ButtonStyles”没有任何构造或调用签名。我究竟做错了什么?谢谢

import React from "react";
import styled from "styled-components";

const PrimaryStyles = styled.button`
  width: 26rem;
  height: 5rem;
  background-image: ${({ theme }) => theme.gradients.ry};
  border: none;
  border-radius: 4px;
  color: ${({ theme }) => theme.black};
  font-size: 2rem;
  box-shadow: 3px 3px 30px ${({ theme }) => theme.red + "70"};
  cursor: pointer;
  padding: 0 1rem;
`;

const SecondaryStyles = styled.button``;

const TertiaryStyles = styled.button`
  border: none;
  background: none;
  color: ${({ theme }) => theme.grey};
`;

interface IProps {
  type: "button" | "reset" | "submit";
  children: React.ReactNode;
  onClick: React.MouseEventHandler<HTMLButtonElement>;
  primary?: boolean;
  secondary?: boolean;
  tertiary?: boolean;
}
export const Button: React.FC<IProps> = ({
  children,
  onClick,
  type,
  primary = true,
  secondary,
  tertiary,
}) => {
  const ButtonStyles = primary
    ? PrimaryStyles
    : secondary
    ? SecondaryStyles
    : tertiary
    ? TertiaryStyles
    : null;
  return (
    <ButtonStyles type={type} onClick={onClick}>
      {children}
    </ButtonStyles>
  );
};

路易斯·保罗·平托

这是因为这个逻辑:

const ButtonStyles = primary
    ? PrimaryStyles
    : secondary
    ? SecondaryStyles
    : tertiary
    ? TertiaryStyles
    : null;

如果不是primarysecondary或者tertiary会是null所以你的代码将设置ButtonStylesNULL然后你不能这样做:

 return (
    <ButtonStyles type={type} onClick={onClick}>
      {children}
    </ButtonStyles>
  );

对此的解决方案是删除该NULL选项,例如:

const ButtonStyles = primary
    ? PrimaryStyles
    : secondary
    ? SecondaryStyles
    : TertiaryStyles;

另一种方法,以避免三元条件运算符将做这样的事情:

  1. 创建一个包含 3 个样式选项的对象:
interface IButtonStyles {
  [key: string]: StyledComponent<"button", any, {}, never>;
}

const buttonStyleTypes: IButtonStyles = {
  primary: PrimaryStyles,
  secondary: SecondaryStyles,
  tertiary: TertiaryStyles,
};
  1. 将您的 IProps 更改为以下内容:
interface IProps {
  type: "button" | "reset" | "submit";
  children: React.ReactNode;
  onClick: React.MouseEventHandler<HTMLButtonElement>;
  styleType?: string;
}
  1. Button 组件:
const Button: React.FC<IProps> = ({
  children,
  type,
  styleType = "primary",
}) => {

  const ButtonStyles = buttonStyleTypes[styleType];

  return <ButtonStyles type={type}>{children}</ButtonStyles>;
};
  1. 最后,您可以像这样创建按钮:
<Button type={"button"}>primary</Button>
<Button type={"button"} styleType='secondary'>secondary</Button>
<Button type={"button"} styleType='tertiary'>tertiary</Button>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 styled-components 中指定从 UI 库导入的组件的 props

styled-components-如何在html或body标签上设置样式?

React Styled-Components-如何根据道具条件或伪类渲染样式?

React Styled-Components 传递道具问题

Styled-components vs Emotion - 如何在 Styled-components 上重新应用 css`style` 函数

使用Styled-Components创建NPM包:Styled-Components TypeError:t.hasOwnProperty不是函数

如何在Meteor环境中使用React,Material UI和Styled-components管理服务器端渲染?

styled-components表示在您的React组件(Component)周围包装了styled()

Styled-Components的.attrs()函数的用例是什么?

@ types / styled-components重复的标识符FormData

React Styled Components-如何访问原始HTML

React Styled-Components:参考其他组件

使用 Styled-Components 檢查多個道具

React Styled-Components CSS向子元素添加!important

Styled Components:同时嵌套和引用其他组件

无法从stylelint / stylelint-config-styled-components获得结果或错误

styled-components:主题中的样式覆盖组件样式

在单文件组件方法中使用 vue-styled-components

nextjs + react-native-web + styled-components:warnOnce

React Styled-Components 3个按钮大小

React Styled Components条件三元运算符

React Styled-Components主题-提供者动态主题

用React Styled-Components导入scss变量的最佳方法?

导入 .less 变量以在 React Styled Components 中使用

如何在 Gatsby 2.0 中使用 styled-components 主题

Styled-Components 的扩展规则不起作用

如何使用 styled-components 更改组件的颜色?

nx React/Next.js styled-components 共享类型声明文件 styled.d.ts

通过自定义组件将 props 添加到 styled-component