检查React组件是否被forwardRef包裹



是否有一种内置的方法可以知道组件是否已经用React.forwardRef封装?

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

使用react-is中的isForwardRef函数。

import {isForwardRef} from 'react-is'
isForwardRef(c)

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

最新更新