SayMore

Alex Loukissas

@futureartist

Just because react is now all hooks, it doesn't mean you can't use classes. A class is a great abstraction that's largely underutilized in react code.

Cameron Armstrong

@cameron

@saymore Can you go through a few examples where you think it makes sense to use classes?

Author Reply ❌

Farcaster Comments

Alex Loukissas

@futureartist

In general operations on an object are good fit. You can think of a class as a superset of an object, that also has functions you can run on it (vs passing an object to a function).

Cameron Armstrong

@cameron

So tangibly can you share a scenario where you’ve taken that approach?

Alex Loukissas

@futureartist

Super super simplistic example below: class AudioFile { readonly url: string; playing: boolean; constructor(url: string) { this.url = url; this.playing = false; } playPause() { this.playing = !this.playing } }

Cameron Armstrong

@cameron

Ah okay - yea I can see how powerful that can be

Alex Loukissas

@futureartist

Reading Fowler's "Refactoring" book re-warmed my preference for classes

Cameron Armstrong

@cameron

@essay Classes vs Hooks

Darryl Yeo 🛠️

@darrylyeo

Would you still recommend class components though? I don’t see much benefit to them.

Alex Loukissas

@futureartist

Oh no, doesn’t make sense today.
Made with ✨ by Cameron