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 应用创建精确的类型定义,这样不仅可以提高代码的可读性和可维护性,还能在编写代码时获得更好的类型检查和自动完成功能。

相关推荐
尘寰ya6 分钟前
如何实现一个“纯净”的空对象(无原型链属性)?
javascript·面试·原型模式
DXM05217 分钟前
牟乃夏《ArcGIS Engine地理信息系统开发教程》学习笔记2
开发语言·javascript·笔记·学习·arcgis·ae
D哈迪斯13 分钟前
vue动态组件实现动态表单的方法
前端·javascript·vue.js
KeyNG_Jykxg13 分钟前
🎨Element Plus X 上新! 组件升级🥳
前端·javascript·vue.js
Monly2119 分钟前
Uniapp:列表选择提示框
开发语言·javascript·uni-app
前端服务区26 分钟前
React内置Hooks
前端·react.js
啊吧啊吧曾小白27 分钟前
聊一聊前端日常使用的try...catch...finally
前端·javascript·面试
海底火旺29 分钟前
前端面试必考!== 和 === 的区别及最佳实践全解析
前端·javascript
几何心凉30 分钟前
企业数据采集新实践:提升工作效率的秘籍
前端·javascript
琦遇35 分钟前
Vue3使用Swiper实现列表内容分两行循环滚动
前端·javascript·vue.js