Checking Class Components In React With IsClassComponent Function
isClassComponent function checks if a React Component is a class component. It returns true if the Component is a function with an isReactComponent property.
In this article, we will review a code snippet from TipTap source code.
/**
* Check if a component is a class component.
* @param Component
* @returns {boolean}
*/
function isClassComponent(Component: any) {
return !!(
typeof Component === 'function'
&& Component.prototype
&& Component.prototype.isReactComponent
)
}
The beauty lies in the comment explaining what this function does. isClassComponent function name is pretty self-explanatory. It checks if a Component is a class component.
You would write a class component in React using this below s...