Sunday, June 17, 2018

better way of knowing if react component has a ref property in typescript

Leave a Comment

I have the following code to try and work out whether a component instance is an instance of a class component and has a ref property:

type Refable<T> = React.ReactElement<T> & {   ref: React.RefObject<T>; };  const componentHasRef = function<T>(component: React.ReactElement<T> | Refable<T>): component is Refable<T> {   return !!(component as any).ref; }; 

Is there a better way than return !!(component as any).ref; of doing this?

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment