亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

轉發 ref 到 react.FC 給出類型錯誤:屬性 ref 不存在于類型

轉發 ref 到 react.FC 給出類型錯誤:屬性 ref 不存在于類型

胡子哥哥 2023-03-10 15:06:30
我在這里學習如何使用前向引用我有一個 FC,我需要在其中初始化所有引用,然后將它們傳遞給它的子項,這樣我就可以獲得一些 chartjs 圖表的畫布實例。但是使用 forwardRef 我得到一個類型錯誤,說 ref 不是孩子的屬性。const BUIDashboard: React.FC = () => {    const chartRef = React.createRef<RefObject<HorizontalBar>>()...    return (      <Child           ref={chartRef} <------------------- TYPE ERROR HERE          isLoading={isLoadingChild}          data={childData} />     )}child沒有錯誤但是是這樣設置的type Props = {  rootProps?: DashboardCardProps  isLoading?: boolean  data?: BUIMetrics['breachBreakdownByOutcome']}const Child: React.FC<Props> = React.forwardRef(({ rootProps, isLoading, data }, ref: RefObject<HorizontalBar>) => {    return (      <HorizontalBar ref={ref}/>    )}我是否錯誤地為孩子定義了參數?我認為問題可能出在這一行const Child: React.FC<Props>所以我將道具類型更新為type Props = {  rootProps?: DashboardCardProps  isLoading?: boolean  data?: BUIMetrics['breachBreakdownByOutcome']} & { ref: RefObject<HorizontalBar> }但是,子組件聲明會拋出此錯誤:TS2322: Type 'ForwardRefExoticComponent<Pick<Props, "rootProps" | "isLoading" | "data"> & RefAttributes<HorizontalBar>>' is not assignable to type 'FC<Props>'.  Types of property 'defaultProps' are incompatible.    Type 'Partial<Pick<Props, "rootProps" | "isLoading" | "data"> & RefAttributes<HorizontalBar>> | undefined' is not assignable to type 'Partial<Props> | undefined'.      Type 'Partial<Pick<Props, "rootProps" | "isLoading" | "data"> & RefAttributes<HorizontalBar>>' is not assignable to type 'Partial<Props>'.        Types of property 'ref' are incompatible.          Type '((instance: HorizontalBar | null) => void) | RefObject<HorizontalBar> | null | undefined' is not assignable to type 'RefObject<HorizontalBar> | undefined'.            Type 'null' is not assignable to type 'RefObject<HorizontalBar> | undefined'.這也是明目張膽的廣告,但我正試圖解決這個問題以解決下面鏈接的另一個問題。如果您對這個問題有任何見解,也請告訴我。謝謝。 
查看完整描述

1 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

type Props = {

  rootProps?: DashboardCardProps

  isLoading?: boolean

  data?: BUIMetrics['breachBreakdownByOutcome']

}


const Child = React.forwardRef<RefObject<HorizontalBar>,Props>(({ rootProps, isLoading, data, children }, ref) => {

    return (

      <HorizontalBar ref={ref}/>

    );

})


查看完整回答
反對 回復 2023-03-10
  • 1 回答
  • 0 關注
  • 259 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號