4 ms·
Separate methods for lifecycle events is probably the biggest weakness of class components. They work well for smaller components, but when you have multiple th
by ratww 7y ago
Separate methods for lifecycle events is probably the biggest weakness of class components. They work well for smaller components, but when you have multiple things going on in a single class you have code related to a single concern scattered around multiple methods, and you start having to use conditionals inside your lifecycle methods.
The article doesn't show pathological cases using classes, but with hooks it becomes possible to extract complex behaviors involving different lifecycles much more cleanly.
I recently migrated a large React project to use hooks and a lot of repetitive code with lots of repeated patterns (especially things related to data access) is now much more cleaner. Now I can just have something like `useFetch` instead of ten lines of code that I needed just because I was using lifecycle methods.
And the fact that the order of hooks matter is what makes them so easy to abstract. Having them store their own "id" would remove the need for that rule, but then you'd be running at other kinds of bugs.