av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

使用React.forwardRef傳遞泛型參數(shù)

瀏覽:170日期:2022-06-10 09:37:12
目錄
  • React.forwardRef傳遞泛型參數(shù)
    • 使用forwardRef暴露組建的方法和屬性
    • 泛型參數(shù)
  • react forwardRef 導致 泛型丟失
    • 實現(xiàn)方式如下
  • 總結

    React.forwardRef傳遞泛型參數(shù)

    使用React函數(shù)組件開發(fā)的過程中會遇到父組件調用子組件的方法或者屬性的場景,首次先說怎么通過React.forwardRef來將子組件的屬性或者方法暴露給父組件

    使用forwardRef暴露組建的方法和屬性

    子組件

    import { Box, Typography } from "@mui/material";
    import { forwardRef, useImperativeHandle } from "react";
    interface LocationChildProps {
      data: string;
    }
    export interface LocationChildRef {
      sayType(): void;
    }
    const LocationChild = forwardRef<LocationChildRef, LocationChildProps>((props, ref) => {
      useImperativeHandle(ref, () => ({
        sayType() {
          console.log("子組件的data是 " + typeof props.data);
        },
      }));
      return (
        <Box>
          <Typography>{typeof props.data}</Typography>
        </Box>
      );
    });
    export default LocationChild;

    在子組件中我們需要接受一個key為data的props,然后在子組件中展示這個值,并且通過useImperativeHandle向外暴露一個sayType的方法, 最后用forwardRef將子組件封裝然后暴露出去,這里forwardRef的作用就是包裝該組件為一個可以通過Ref訪問的組件。

    父組件

    import { Button } from "@mui/material";
    import { useRef } from "react";
    import ConfigDetailContainer from "../options/ConfigDetailContainer";
    import LocationChild, { LocationChildRef } from "./LocationChild";
    export default function DeviceLocation() {
      const locationChildRef = useRef<LocationChildRef>();
      const handleClick = () => {
        locationChildRef.current.sayType()
        // 輸出: 子組件的type是 string  
      };
      return (
        <ConfigDetailContainer title="device.configTabs.LOCATION_HISTORY">
          <LocationChild ref={locationChildRef} data="asdafaf"></LocationChild>
          <Button onClick={handleClick}>查看子組件的type的類型</Button>
        </ConfigDetailContainer>
      );
    }

    父組件中需要通過useRef來創(chuàng)建ref并傳遞給子組件,這樣父子組件就建立了連接,父組件可以通過ref來訪問子組件中自定義暴露的屬性或方法。

    這里的操作就是父組件點擊按鈕控制臺打印子組件接收到的data這個prop的類型。

    泛型參數(shù)

    現(xiàn)在新的問題就是我們的父組件傳遞的data的類型不是固定的,這時候子組件就要將data的類型用泛型來定義,所以這里就有了fowardRef傳遞泛型參數(shù)的問題:

    我們可以這樣改造子組件,思路就是將這個組件改為工廠hansh的生成模式:

    import { Box, Typography } from "@mui/material";
    import { forwardRef, useImperativeHandle } from "react";
    export interface LocationChildProps<T = string> {
      data: T;
    }
    export interface LocationChildRef {
      sayType(): void;
    }
    const LocationChild = function <T>() {
      return forwardRef<LocationChildRef, LocationChildProps<T>>((props, ref) => {
        useImperativeHandle(ref, () => ({
          sayType() {
    console.log("子組件的data是 " + typeof props.data);
          },
        }));
        return (
          <Box>
    <Typography>{typeof props.data}</Typography>
          </Box>
        );
      });
    };
    export default LocationChild;

    然后在父組件中使用

    import { Button } from "@mui/material";
    import { PropsWithRef, useRef } from "react";
    import ConfigDetailContainer from "../options/ConfigDetailContainer";
    import LocationChild, { LocationChildProps, LocationChildRef } from "./LocationChild";
    export default function DeviceLocation() {
      const locationChildRefString = useRef<LocationChildRef>();
      const locationChildRefBoolean = useRef<LocationChildRef>();
      const handleClick = () => {
        locationChildRefString.current.sayType();
        locationChildRefBoolean.current.sayType();
      };
      const LocationChildComponent = LocationChild<string>();
      const createComponent = function <T>(props: PropsWithRef<any>, ref: React.MutableRefObject<LocationChildRef>) {
        const Mycomponent = LocationChild<T>();
        return <Mycomponent ref={ref} {...props}></Mycomponent>;
      };
      return (
        <ConfigDetailContainer title="device.configTabs.LOCATION_HISTORY">
          <LocationChildComponent ref={locationChildRefString} data={"123"}></LocationChildComponent>
          {createComponent({ data: true }, locationChildRefBoolean)}
          <Button onClick={handleClick}>查看子組件的type的類型</Button>
        </ConfigDetailContainer>
      );
    }

    我們可以直接調用LocationChild方法生成組件,也可以再度封裝為createComponent這樣的方法,這樣就實現(xiàn)了forwardRef中使用泛型參數(shù)的需求。

    react forwardRef 導致 泛型丟失

    網(wǎng)上沒有找到合適的方案,看了 antd 的源碼

    實現(xiàn)方式如下

    const ForwardTable = React.forwardRef(InternalTable) as <RecordType extends object = any>(
    ? props: React.PropsWithChildren<TableProps<RecordType>> & { ref?: React.Ref<HTMLDivElement> },
    ) => React.ReactElement;
    // so u can use
    <Table<{id: string, b: number}> ?/>

    總結

    以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持。

    標簽: JavaScript
    相關文章:
    主站蜘蛛池模板: 国产精品久久久久久吹潮 | 亚洲国产精品网站 | 欧美亚洲国产一区二区三区 | 人人澡人人射 | 男人的天堂在线视频 | 亚洲九九| 嫩草视频网 | 91亚洲视频在线 | 欧美一级网站 | 作爱视频免费观看 | 青青激情网 | 国产成人a亚洲精品 | 久久这里只有 | 亚洲天堂中文字幕 | 五月天国产在线 | 日本欧美在线观看视频 | 久久久久久久一区二区三区 | 精品啪啪 | 99久久99久久精品国产片果冰 | 日韩在线一区二区 | h在线免费观看 | 精品久久久久久久久久久久 | 97久久久久久 | 中文字幕日韩欧美一区二区三区 | 91免费福利在线 | 国产福利二区 | 欧美日韩a| 国产一区二区三区亚洲 | 91精品欧美久久久久久久 | 69av网 | 色婷婷婷婷色 | 国产福利精品一区 | 欧美日韩中文字幕在线 | 国产精品久久久久久久久久久久久 | 日韩一区二区三区精品 | 91精品一区 | 亚洲视频在线观看 | 91久久久久 | 久久久精品 | 天天综合日日夜夜 | 欧美色综合一区二区三区 |