簡體   English   中英

檢查一個 React 組件是否被 forwardRef 包裹

[英]Check if a React component is wrapped by forwardRef

是否有內置方法可以知道組件是否已經用React.forwardRef包裝?

test(React.forwardRef(() => null))

function test(c) {
  const isForwardRef = ???
  if (!isForwardRef) {
    c = React.forwardRef(c)
  }
  // ...
}

使用來自react-isisForwardRef function 。

import {isForwardRef} from 'react-is'

isForwardRef(c)

https://github.com/facebook/react/blob/d48dbb824985166ecb7b2959db03090a8593dce0/packages/react-is/src/ReactIs.js#L119-L121

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM