React 和 TypeScript 联合使用,React 提供了一些内置类型

在 React 和 TypeScript 联合使用时,React 提供了一些内置类型来帮助你定义组件的属性(props)、状态(state)和其他常见的模式。以下是一些详细的内置类型说明:

常用的 React 类型

  1. React.FC / React.FunctionComponent

    • 用于定义函数组件的类型。

    • 它包含 children 属性,即使你没有显式地在你的类型定义中声明它们。

    • 例子:

      tsx 复制代码
      interface MyComponentProps {
        title: string;
      }
      
      const MyComponent: React.FC<MyComponentProps> = ({ title }) => (
        <div>{title}</div>
      );
  2. React.Component / React.PureComponent

    • 用于定义类组件的类型。

    • React.Component<Props, State> 接受两个泛型参数,第一个是 props 的类型,第二个是 state 的类型。

    • React.PureComponent 类似于 React.Component,但它通过浅比较 props 和 state 来帮助避免不必要的渲染。

    • 例子:

      tsx 复制代码
      interface MyComponentProps { /* ... */ }
      interface MyComponentState { /* ... */ }
      
      class MyComponent extends React.Component<MyComponentProps, MyComponentState> {
        /* ... */
      }
  3. React.ReactNode

    • 表示任何可以被渲染的内容,包括 JSX 元素、字符串、数字、null、布尔值等。

    • 例子:

      tsx 复制代码
      const content: React.ReactNode = 'This is a text node';
      const element: React.ReactNode = <div>{content}</div>;
  4. React.ReactElement

    • 表示一个 JSX 元素。

    • 通常在渲染函数中返回 JSX 时使用。

    • 例子:

      tsx 复制代码
      const element: React.ReactElement = <div>Hello World</div>;
  5. React.CSSProperties

    • 用于定义传递给 style 属性的对象类型。

    • 例子:

      tsx 复制代码
      const style: React.CSSProperties = { color: 'red', fontSize: 20 };
  6. React.ReactEventHandler

    • 用于定义处理事件的函数类型,如点击、改变等。

    • 例子:

      tsx 复制代码
      const handleClick: React.ReactEventHandler<HTMLButtonElement> = (event) => {
        // ...
      };
  7. React.Ref

    • 用于定义引用类型,可以引用 DOM 元素或类组件的实例。

    • 例子:

      tsx 复制代码
      const myRef = React.useRef<HTMLDivElement>(null);

事件处理相关类型

  1. React.ChangeEvent

    • 用于 onChange 事件处理器中的事件对象。

    • 例子:

      tsx 复制代码
      const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        console.log(event.currentTarget.value);
      };
  2. React.FormEvent

    • 用于表单事件,如提交表单。

    • 例子:

      tsx 复制代码
      const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
        event.preventDefault();
        // ...
      };

高级类型工具

  1. React.ComponentProps

    • 用于获取组件的 props 类型。

    • 例子:

      tsx 复制代码
      type MyComponentProps = React.ComponentProps<typeof MyComponent>;
  2. React.ComponentPropsWithRef

    • 类似于 React.ComponentProps,但也包括 ref 属性。

    • 例子:

      tsx 复制代码
      type MyComponentProps = React.ComponentPropsWithRef<typeof MyComponent>;
  3. React.ComponentPropsWithoutRef

    • 类似于 React.ComponentProps,但不包括 ref 属性。

    • 例子:

      tsx 复制代码
      type MyComponentProps = React.ComponentPropsWithoutRef<typeof MyComponent>;

使用这些内置类型,你可以为你的 React 应用创建精确的类型定义,这样不仅可以提高代码的可读性和可维护性,还能在编写代码时获得更好的类型检查和自动完成功能。

相关推荐
有事没事实验室2 分钟前
router-link的custom模式
前端·javascript·vue.js
4***V2023 分钟前
Vue3响应式原理详解
开发语言·javascript·ecmascript
Tonychen11 分钟前
TypeScript 里 infer 常见用法
前端·typescript
O***p6041 小时前
JavaScript在Node.js中的集群负载均衡
javascript·node.js·负载均衡
xhxxx1 小时前
prototype 是遗产,proto 是族谱:一文吃透 JS 原型链
前端·javascript
蚂蚁集团数据体验技术3 小时前
一个可以补充 Mermaid 的可视化组件库 Infographic
前端·javascript·llm
Lethehong3 小时前
简历优化大师:基于React与AI技术的智能简历优化系统开发实践
前端·人工智能·react.js·kimi k2·蓝耘元生代·蓝耘maas
华仔啊3 小时前
还在用 WebSocket 做实时通信?SSE 可能更简单
前端·javascript
木易士心4 小时前
深入剖析:按下 F5 后,浏览器前端究竟发生了什么?
前端·javascript
xump5 小时前
如何在DevTools选中调试一个实时交互才能显示的元素样式
前端·javascript·css