|
|
|
|
|
by vmind
2377 days ago
|
|
I don't think it's fair to say React hooks are optimising for writability, even if they are much shorter, but modularity. While lifecycle methods are 'obvious and straightforward', they often spread related functionality across multiple different functions, which can be more easily grouped together with hooks. And if you have similar lifecycle-based functionality that needs to be shared between multiple components, you end up with a variety of not-great solutions, whereas that is simple with hooks. That's not to say hooks aren't without a downside, it is more to learn, with a different mental model, and their own edge cases. But having used hooks exclusively for a larger project, I couldn't imagine going back to lifecycle methods. |
|
Interestingly, useMemo doesn't even need to be defined in React in a class-based component:
This is also just one of the possible APIs. I'm sure there are variants of this which are more performant and/or easier to work with.