欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react常見(jiàn)的ts類(lèi)型實(shí)踐解析

 更新時(shí)間:2023年04月16日 10:30:33   作者:Maic  
這篇文章主要為大家介紹了react常見(jiàn)的ts類(lèi)型實(shí)踐解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

ts在中在react用處很是很廣泛,本文是一篇關(guān)于在react中常用的類(lèi)型總結(jié),希望能帶來(lái)一些思考和幫助。

  • 一個(gè)函數(shù)組件
import React from "react";
type Props = {
}
const Header: React.FC<Props> = (props) => {
  return (<>
    <div>header</div>
    {props.children}
  </>)
}

我們注意在Header組件中有使用到props.children如果Props沒(méi)有申明類(lèi)型那么此時(shí)就會(huì)報(bào)這樣的錯(cuò)誤

此時(shí)我們需要加個(gè)類(lèi)型就行,并且children是可選的

import React from "react";
interface Props {
  children?: React.ReactNode;
}

除了children,有時(shí)我想給Header組件傳入一個(gè)className,并且是可選的

import React from "react";
type Props = {
 children?: React.ReactNode;
 className?: string;
}
const Header: React.FC<Props> = (props) => {
  const { className } = props;
  return (<>
    <div className={`App-header ${className}`}>header</div>
    {props.children}
  </>)
}

Props我們似乎對(duì)每一個(gè)可選項(xiàng)都有做?可選,有沒(méi)有一勞永逸的辦法

Partial<T>所有屬性都是可選

import React from "react";
type Props = {
 children: React.ReactNode;
 className: string;
}
const Header: React.FC<Partial<Props>> = (props) => {
  const { className = '' } = props;
  return (<>
    <div className={`App-header ${className}`}>header</div>
    {props.children}
  </>)
}

在以上我們給Props申明了一個(gè)children?: React.ReactNode,如果你不想這么寫(xiě),react也提供了一個(gè)屬性PropsWithChildren

interface ChildProps {}
export const SubHeader: React.FC = (
  props: PropsWithChildren<{}> & Partial<ChildProps>
) => {
  return <div className={`sub-header`}>{props.children}</div>;
};

在dom節(jié)點(diǎn)上的類(lèi)型

import React, { PropsWithChildren, useRef } from "react";
const Input: React.FC = () => {
  const inputRef = useRef<HTMLInputElement>(null);
  const sureRef = useRef<HTMLDivElement>(null);
  return (
    <>
      <input type="text" ref={inputRef} />
      <div ref={sureRef}>確定</div>
    </>
  );
};

傳入子組件的方法

我想傳入一個(gè)方法到子組件里去

type InputProps = {
  onSure: () => void;
};
const Input: React.FC<InputProps> = (props) => {
  const inputRef = useRef<HTMLInputElement>(null);
  const sureRef = useRef<HTMLDivElement>(null);
  return (
    <>
      <input type="text" ref={inputRef} />
      <div ref={sureRef} onClick={props?.onSure}>
        確定
      </div>
    </>
  );
};
const Index: React.FC<Partial<Props>> = (props) => {
  const { className } = props;
  const handleSure = () => {};
  return (
    <header className={`App-header ${className}`}>
      <Input onSure={handleSure} />
      {props.children}
    </header>
  );
};

!非空斷言,一定有該方法或者屬性

  const body = document!.getElementsByTagName("body")[0];
  body.addEventListener("click", () => {
    console.log("body");
  });

一個(gè)doms上的類(lèi)型

sure按鈕上用ref綁定一個(gè)dom

const Input: React.FC<InputProps> = (props) => {
  const inputRef = useRef<HTMLInputElement>(null);
  const sureRef = useRef(null);
  const body = document!.getElementsByTagName("body")[0];
  body.addEventListener("click", () => {
    console.log(sureRef.current?.style);
    console.log("body");
  });
  return (
    <>
      <input type="text" ref={inputRef} />
      <div ref={sureRef} onClick={props?.onSure}>
        確定
      </div>
    </>
  );
};

此時(shí)我們需要給sureRef申明類(lèi)型,并且?訪問(wèn)可選屬性

const inputRef = useRef<HTMLInputElement>(null);
const sureRef = useRef<HTMLDivElement>(null);
const body = document!.getElementsByTagName("body")[0];
body.addEventListener("click", () => {
  console.log(sureRef.current?.style);
  console.log("body");
});

導(dǎo)入一個(gè)組件需要的多個(gè)類(lèi)型

// userInterfence.ts
export type UserInfo = {
  name: string;
  age: number;
};
export type Menu = {
  title: string;
  price: number;
  isChecked: boolean;
  items: Array<{
    name: string;
    price: number;
  }>;
};

在另外一個(gè)組件引入

import type { UserInfo, Menu } from "./userInterfence";
const Input: React.FC<InputProps> = (props) => {
  const [userInfo, setUserInfo] = useState<UserInfo>({
    name: "Web技術(shù)學(xué)苑",
    age: 10,
  });
  const inputRef = useRef<HTMLInputElement>(null);
  const sureRef = useRef<HTMLDivElement>(null);
  const body = document!.getElementsByTagName("body")[0];
  body.addEventListener("click", () => {
    console.log(sureRef.current?.style);
    console.log("body");
  });
  return (
    <>
      <input type="text" ref={inputRef} value={userInfo.name} />
      <input type="text" value={userInfo.age} />
      <div ref={sureRef} onClick={props?.onSure}>
        確定
      </div>
    </>
  );
};

選擇一個(gè)組件的指定的幾個(gè)屬性

在兩個(gè)類(lèi)似的組件,我們有一些公用的屬性,此時(shí)我們的類(lèi)型可以借用Omit去掉一些不需要的屬性類(lèi)型

import type { UserInfo, Menu } from "./userInterfence";
const MenuComp: React.FC<Omit<Menu, "items" | "isChecked">> = (props) => {
  return (
    <>
      <p>{props.price}</p>
      <p>{props.title}</p>
    </>
  );
};

header組件中引入

<header className={`App-header ${className}`}>
    <MenuComp price={10} title={"menuA"} />
    {props.children}
  </header>

或者你可以使用Pick來(lái)選擇指定的屬性

import type { UserInfo, Menu } from "./userInterfence";
const MenuSubComp: React.FC<Pick<Menu, "items">> = (props) => {
  return (
    <>
      <p>{props.items[0].name}</p>
      <p>{props.items[0].price}</p>
    </>
  );
};

另一個(gè)組件中使用

const Index: React.FC<Partial<Props>> = (props) => {
  const { className } = props;
  const subInfo: Pick<Menu, "items"> = {
    items: [
      {
        name: "Web技術(shù)學(xué)苑",
        price: 10,
      },
    ],
  };
  return (
    <header className={`App-header ${className}`}>
      <MenuComp price={10} title={"menuA"} />
      <MenuSubComp items={subInfo.items} />
      {props.children}
    </header>
  );
};

總結(jié)

react高頻常用的ts,比如如何申明一個(gè)組件的返回的類(lèi)型,以及接收props的參數(shù)

如何申明一個(gè)dom上的類(lèi)型,以及如何傳入一個(gè)函數(shù)到子組件的類(lèi)型

!?的使用,當(dāng)我們知道一定有該屬性時(shí),你可以使用!,如果一個(gè)屬性是可選的那么就用?

OmitPick在組件中的使用,更多typescript參考官方文檔學(xué)習(xí)

本文code example

以上就是react常見(jiàn)的ts類(lèi)型實(shí)踐解析的詳細(xì)內(nèi)容,更多關(guān)于react常見(jiàn)的ts類(lèi)型的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • styled-components?性能詳解

    styled-components?性能詳解

    這篇文章主要為大家介紹了styled-components?的性能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • ReactiveCocoa代碼實(shí)踐之-UI組件的RAC信號(hào)操作

    ReactiveCocoa代碼實(shí)踐之-UI組件的RAC信號(hào)操作

    這篇文章主要介紹了ReactiveCocoa代碼實(shí)踐之-UI組件的RAC信號(hào)操作 的相關(guān)資料,需要的朋友可以參考下
    2016-04-04
  • react實(shí)現(xiàn)Radio組件的示例代碼

    react實(shí)現(xiàn)Radio組件的示例代碼

    這篇文章主要介紹了react實(shí)現(xiàn)Radio組件的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • React通過(guò)父組件傳遞類(lèi)名給子組件的實(shí)現(xiàn)方法

    React通過(guò)父組件傳遞類(lèi)名給子組件的實(shí)現(xiàn)方法

    React 是一個(gè)用于構(gòu)建用戶界面的 JAVASCRIPT 庫(kù)。這篇文章主要介紹了React通過(guò)父組件傳遞類(lèi)名給子組件的方法,需要的朋友可以參考下
    2017-11-11
  • react項(xiàng)目中如何引入國(guó)際化

    react項(xiàng)目中如何引入國(guó)際化

    在React項(xiàng)目中引入國(guó)際化可以使用第三方庫(kù)來(lái)實(shí)現(xiàn),本文主要介紹了react項(xiàng)目中如何引入國(guó)際化,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • React?Refs?的使用forwardRef?源碼示例解析

    React?Refs?的使用forwardRef?源碼示例解析

    這篇文章主要為大家介紹了React?之?Refs?的使用和?forwardRef?的源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 使用React-Window實(shí)現(xiàn)虛擬滾動(dòng)效果的示例代碼

    使用React-Window實(shí)現(xiàn)虛擬滾動(dòng)效果的示例代碼

    React-Window?是一個(gè)為?React?應(yīng)用程序中高效渲染大數(shù)據(jù)集而設(shè)計(jì)的庫(kù),它基于窗口化或虛擬化的原則運(yùn)行,本文將使用React-Window實(shí)現(xiàn)虛擬滾動(dòng)效果,感興趣的可以了解下
    2024-01-01
  • React函數(shù)組件hook原理及構(gòu)建hook鏈表算法詳情

    React函數(shù)組件hook原理及構(gòu)建hook鏈表算法詳情

    這篇文章主要介紹了React函數(shù)組件hook原理及構(gòu)建hook鏈表算法詳情,每一個(gè)?hook?函數(shù)都有對(duì)應(yīng)的?hook?對(duì)象保存狀態(tài)信息,更多詳細(xì)分析,需要的朋友可以參考一下
    2022-07-07
  • 解決React報(bào)錯(cuò)Property does not exist on type 'JSX.IntrinsicElements'

    解決React報(bào)錯(cuò)Property does not exist on 

    這篇文章主要為大家介紹了React報(bào)錯(cuò)Property does not exist on type 'JSX.IntrinsicElements'解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • React初始化渲染過(guò)程示例詳解

    React初始化渲染過(guò)程示例詳解

    這篇文章主要為大家介紹了React初始化渲染過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09

最新評(píng)論