Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a React component is wrapped by forwardRef

Tags:

reactjs

Is there a built-in way to know if a component is already wrapped with React.forwardRef?

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

function test(c) {
  const isForwardRef = ???
  if (!isForwardRef) {
    c = React.forwardRef(c)
  }
  // ...
}
like image 828
aleclarson Avatar asked Oct 17 '25 19:10

aleclarson


1 Answers

Use the isForwardRef function from react-is.

import {isForwardRef} from 'react-is'

isForwardRef(c)

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

like image 189
aleclarson Avatar answered Oct 20 '25 09:10

aleclarson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!