Preact: Check if component is Stateless or Class-Based

Created on 16 Sep 2017  路  2Comments  路  Source: preactjs/preact

I saw on React there is a flag to check current component is a class or a stateless component.
Here it is, .isReactComponent

Currently, I'm trying to make a preact tree walker to fetch data on the server (SSR), therefore I make a recursive function to walk on every preact component, and now I'm struggling to check if the current component is stateless or class-based (I'm using react-router v4)

Thanks

Most helpful comment

Hi @pl12133
I never had a thought that I can use Component.prototype.render to check the component is class-based or not.

Thanks, @pl12133

All 2 comments

Hey @alfrcr thanks for the issue. Checking for the existence of the render function is an acceptable way to determine if a component is a class Component. This method is used in acdlite/recompose to detect if a component is a class, see https://github.com/acdlite/recompose/commit/1c9cf7b1ac75fb1a65a4db9a5a0fe2344ad97c2d.

I think the isClassComponent function from recompose will be able to solve your problem.

Hi @pl12133
I never had a thought that I can use Component.prototype.render to check the component is class-based or not.

Thanks, @pl12133

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kossnocorp picture kossnocorp  路  3Comments

nopantsmonkey picture nopantsmonkey  路  3Comments

marcosguti picture marcosguti  路  3Comments

k15a picture k15a  路  3Comments

philipwalton picture philipwalton  路  3Comments