当前位置: 首页 > news >正文

React 与 TS 结合使用时的技巧总结

使用 TS 泛型来定义通用参数

有些时候会遇到有些业务页面结构是通用的,只是传入页面组件的参数略有不同,这样我们可以使用 TS 的泛型来定义通用参数。具体的实例如下:

type GenericPropsData<T> = {items: T[];onClick: (value: T) => void;
};const GenericProps: React.FC<GenericPropsData<number | string>> = ({items,onClick,
}) => {return (<div><h2>TS 泛型定义参数</h2>{items.map((item) => {return (<div key={item} onClick={() => onClick(item)}>{item}</div>);})}</div>);
};export default GenericProps;

上述例子中,只是简单列举了泛型为数字或者字符串,泛型还可以定位为其他对象。

使用 TS 限定传入的参数

有些业务场景要求根据在一定条件下传入对应参数,组件中的其他参数为不能传递的情况,或者出现类型的情况时,我们可以考虑使用 TS 的never联合类型来声明定义参数的组件。具体实例如下:

type RandomNumberType = {value: number;
};type PositiveNumber = RandomNumberType & {isPositive: boolean;isNegative?: never;isZero?: never;
};type NegativeNumber = RandomNumberType & {isNegative: boolean;isPositive?: never;isZero?: never;
};type Zero = RandomNumberType & {isZero: boolean;isPositive?: never;isNegative?: never;
};type RandomNumberProps = PositiveNumber | NegativeNumber | Zero;const RestrictionProps: React.FC<RandomNumberProps> = ({value,isPositive,isNegative,isZero,
}) => {return (<div>{value} {isPositive && "整数"} {isNegative && "负数"} {isZero && "0"}</div>);
};export default RestrictionProps;

使用 TS 的 Exclude 去除某些联合类型

我们可以使用 TS 中的 Exclude 来去除某些联合类型的参数,例如下面实例:

type HorizontalPosition = "left" | "center" | "right";
type VerticalPosition = "top" | "center" | "bottom";/*** 组件传入的参数可以有如下这些* "left-center" | "left-top" | "left-bottom" | "center" | "center-top" |* "center-bottom" | "right-center" | "right-top" | "right-bottom"** 我们通过Exclude抛掉了center-center的值*/
type ToastProps = {position:| Exclude<`${HorizontalPosition}-${VerticalPosition}`, "center-center">| "center";
};const ExcludeProps: React.FC<ToastProps> = ({ position }) => {return <div>Toast Notification Position - {position}</div>;
};export default ExcludeProps;

使用 TS 实现多态组件

type TextOwnProps<E extends React.ElementType> = {size?: "sm" | "md" | "lg";color?: "primary" | "secondary";children: React.ReactNode;as?: E;
};type TextProps<E extends React.ElementType> = TextOwnProps<E> &Omit<React.ComponentProps<E>, keyof TextOwnProps<E>>;const PolymorphismProps: React.FC<TextProps<React.ElementType>> = ({size,color,children,as,
}) => {const Component = as || "div";return (<Component className={`class-with-${size}-${color}`}>{children}</Component>);
};export default PolymorphismProps;
http://www.lryc.cn/news/160561.html

相关文章:

  • 【深入解析spring cloud gateway】07 自定义异常返回报文
  • 如何写一个sh脚本将一个本地文件通过 scp命令上传到远程的 centos服务器?
  • 【CMake工具】工具CMake编译轻度使用(C/C++)
  • 用Navicat备份Mysql演示系统数据库的时候出:Too Many Connections
  • 知识储备--基础算法篇-矩阵
  • Zabbix -- QQ邮箱报警
  • eclipse链接MySQL数据库
  • ansible 使用roles简单部署LAMP平台
  • 如何使用Web Storage对页面中数据进行监听?
  • GO语言网络编程(并发编程)runtime包
  • MR源码解析和join案例
  • ML+LLMs:利用LLMs大语言模型赋能或者结合ML机器学习算法进行具体应用的简介、具体案例之详细攻略
  • python GIL锁
  • git打tag和版本控制规范
  • php版 短信跳转微信小程序
  • leetcode127单词接龙刷题打卡
  • 基于SSM的物流管理系统
  • EagleSDR USB HAT FT600
  • Java多线程(四)锁策略(CAS,死锁)和多线程对集合类的使用
  • 基于spring boot+ vue+ mysql开发的UWB室内外定位系统源码
  • 第2章_瑞萨MCU零基础入门系列教程之面向过程与面向对象
  • 数字图像处理:亮度对比度-几何变换-噪声处理
  • maven报错:[ERROR] 不再支持源选项 7。请使用 8 或更高版本。
  • MySQL基础3-约束
  • OJ练习第166题——课程表(拓扑排序问题)
  • 单臂路由实现VLAN间路由
  • 【VSCode】文件模板创建及使用.md
  • 【漏洞复现】EnjoySCM存在文件上传漏洞
  • MaPLe: Multi-modal Prompt Learning
  • 软件测试/测试开发丨Jenkins Pipeline 学习笔记