DEV Community

Discussion on: React: Class Component VS Function Component with Hooks

Collapse
 
simonrobertson profile image
Simon Robertson

I prefer to use class components, but it's personal preference more than anything. Class components and function components (with hooks) both have pros and cons.

One of the problems with class components is dealing with callback scope, i.e. what "this" references, but that can be handled easily these days by attaching a simple decorator to the callback methods. This isn't an issue for function components.

One of the problems with function components (with hooks) is every time the component is rendered, nearly every function in the component (either top-level or passed into a hook) has to be recreated, and that will cascade down through the hooks you use. That can end up being quite painful for performance and/or the garbage collector. This isn't an issue for class components.

You can do almost everything with class components as you can with function components, and vise-versa, so which one people decide to use is personal preference and/or an architectural design decision. I don't believe one is significantly worse than the other.