溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么使用TypeScript開發(fā)React函數(shù)式組件

發(fā)布時間:2022-08-29 11:15:31 來源:億速云 閱讀:157 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了怎么使用TypeScript開發(fā)React函數(shù)式組件的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇怎么使用TypeScript開發(fā)React函數(shù)式組件文章都會有所收獲,下面我們一起來看看吧。

前言

在我們使用 React 開發(fā)項目時,使用最多的應(yīng)該都是組件,組件又分為函數(shù)組件類組件,我們可以這么定義:

定義函數(shù)組件:

function Welcome(props) {
  return <h2>Hello, {props.name}</h2>;
}

定義類組件:

class Welcome extends React.Component {
  render() {
    return <h2>Hello, {this.props.name}</h2>;
  }
}

這篇文章我會和大家介紹使用 TypeScript 定義函數(shù)式組件的 4 種方法,還有幾個使用過程中需要注意的問題。

如何使用 TypeScript 定義函數(shù)式組件

函數(shù)式組件通常接受一個 props 參數(shù),返回一個 JSX 元素或者 null。

當(dāng)我們需要使用 TypeScript 去定義一個函數(shù)式組件時,我們有 4 種方式,4 種方式各有各的優(yōu)缺點(diǎn),看具體情況使用。

1. 使用 React.FC

由于 React 不是使用 TypeScript 開發(fā)的,使用的是社區(qū)開發(fā)的 @type/react 包提供的類型,里面有一個通用類型 FC ,允許我們?yōu)楹瘮?shù)組件添加類型。

type FCProps = { text: string };
// React.FunctionComponent 的簡寫
const FCComponent: React.FC<FCProps> = ({ text = "" }) => <div>{text}</div>;

這里的 React.FC 是 React.FunctionComponent 的簡寫。

當(dāng)組件包含子元素,TypeScript 會提示警告:

type FCProps = { text: string };
const FCComponent: React.FC<FCProps> = ({ text = "" }) => <div>{text}</div>;

function App() {
  return (
    <div className="App">
        <FCComponent text="Hello Chris1993.">
            <span>children</span>
        </FCComponent>
    </div>
  );
}

提示警告內(nèi)容:

Type '{ children: string; text: string; }' is not assignable to type 'IntrinsicAttributes & FCProps'.
  Property 'children' does not exist on type 'IntrinsicAttributes & FCProps'.

2. 使用 JSX.Element

使用 JSX.Element 類型作為函數(shù)式組件的返回值類型,當(dāng)組件的返回值不是 JSX.Element 類型時,TypeScript 就會提示錯誤。

type FCProps = { text: string };
const ElementComponent = ({ text }: FCProps): JSX.Element => <div>{text}</div>;
function App() {
  return (
    <div className="App">
        <ElementComponent text="Hello Chris1993."></ElementComponent>
    </div>
  );
}

3. 直接定義完整類型

由于 React 組件包含子元素時,會隱式傳遞一個 children 屬性,導(dǎo)致定義的參數(shù)類型出錯,因此我們可以直接定義一個完整的參數(shù)接口,包含了 children 屬性的類型:

type FCProps = { text: string; children?: any };
const FCComponent: React.FC<FCProps> = ({ text = "" }) => <div>{text}</div>;

function App() {
  return (
    <div className="App">
        <FCComponent text="Hello Chris1993.">
            <span>children</span>
        </FCComponent>
    </div>
  );
}

4. 使用 React.PropsWithChildren

第 3 種方法每次都要手動寫一個 children 屬性類型比較麻煩,這時候我們就可以使用 React.PropsWithChildren 類型,它本身封裝了 children 的類型聲明:

// react/index.d.ts
type PropsWithChildren<P> = P & { children?: ReactNode };

因此,使用 React.PropsWithChildren 類型定義函數(shù)式組件,就不用去處理 children 的類型了:

type IProps = React.PropsWithChildren<{ text: string }>;
const PropsComponent = ({ text }: IProps) => <div>{text}</div>;
function App() {
  return (
    <div className="App">
        <PropsComponent text="Hello Chris1993.">
            <span>children</span>
        </PropsComponent>
    </div>
  );
}

使用過程需要注意的點(diǎn)

1. 函數(shù)式組件返回值不能是布爾值

當(dāng)我們在函數(shù)式組件內(nèi)使用條件語句時,如果返回的是非 JSX 元素或者非 null 的值,React 將會報錯:

const ConditionComponent = ({ useRender = false }) =>
  useRender ? <span>Render ConditionComponent</span> : false;// ?

function App() {
  return (
    <div className="App">
        <ConditionComponent useRender></ConditionComponent>
        {/* 'ConditionComponent' cannot be used as a JSX component.
            Its return type 'false | Element' is not a valid JSX element.
            Type 'boolean' is not assignable to type 'ReactElement<any, any>'.
        */}
    </div>
  );
}

正確的處理方式,應(yīng)該是讓函數(shù)式組件返回一個有效的 JSX 元素或者 null:

const ConditionComponent = ({ useRender = false }) =>
  useRender ? <span>Render ConditionComponent</span> : <span>error</span>;// ?

// or

const ConditionComponent = ({ useRender = false }) =>
  useRender ? <span>Render ConditionComponent</span> : null;// ?

當(dāng)然你也不能這樣寫,當(dāng)屬性 useRender 為 true 時,也會出錯:

const ConditionComponent = ({ useRender = false }) =>
  useRender && <span>Render ConditionComponent</span>;// ?

2. 無法為組件使用 Array.fill() 填充

當(dāng)我們的組件直接返回 Array.fill() 的結(jié)果時,TypeScript 會提示錯誤。

const ArrayComponent = () => Array(3).fill(<span>Chris1993</span>); // ?

function App() {
  return (
    <div className="App">
      <ArrayComponent></ArrayComponent>
    </div>
  );
}

提示下面內(nèi)容:

'ArrayComponent' cannot be used as a JSX component.
  Its return type 'any[]' is not a valid JSX element.
    Type 'any[]' is missing the following properties from type 'ReactElement<any, any>': type, props, key

為了解決這個問題,我們可以定義函數(shù)的返回值類型:

const ArrayComponent = () =>
  Array(3).fill(<span>Chris1993</span>) as any as JSX.Element; // ?

3. 支持使用泛型來創(chuàng)建組件

在使用 TypeScript 開發(fā) React 函數(shù)式組件的時候,也可以使用泛型進(jìn)行約束,聲明一個泛型組件(Generic Components),這樣可以讓我們的組件更加靈活。

可以這樣使用:

interface GenericProps<T> {
  content: T;
}
const GenericComponent = <T extends unknown>(props: GenericProps<T>) => {
  const { content } = props;
  const component = <>{content}</>;
  return <div>{component}</div>;
};
function App() {
  return (
    <div className="App">
      { /* Success ? */}
      <GenericComponent<number> content={10} />
      { /* Error ? Type 'string' is not assignable to type 'number'. */}
      <GenericComponent<number> content={"10"} />
    </div>
  );
}

在 Generic Components 章節(jié)中介紹到更高級的使用方式:

interface Props<T> {
  items: T[];
  renderItem: (item: T) => React.ReactNode;
}

const List = <T extends unknown>(props: Props<T>) => {
  const { items, renderItem } = props;
  const [state, setState] = React.useState<T[]>([]); // You can use type T in List function scope.
  return (
    <div>
      {items.map(renderItem)}
      <button onClick={() => setState(items)}>Clone</button>
      {JSON.stringify(state, null, 2)}
    </div>
  );
};
function App() {
  return (
    <div className="App">
        <List<number>
          items={[1, 2]} // type of 'string' inferred
          renderItem={(item) => (
            <li key={item}>
              {/* Error: Property 'toPrecision' does not exist on type 'string'. */}
              {item.toPrecision(3)}
            </li>
          )}
        />
    </div>
  );
}

關(guān)于“怎么使用TypeScript開發(fā)React函數(shù)式組件”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“怎么使用TypeScript開發(fā)React函數(shù)式組件”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI