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.
judofyr|6 years ago
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.acemarke|6 years ago
But, the React team deliberately opted to only implement hooks for function components for a few reasons:
- Function components didn't have capability parity with class components in terms of having state and side effects
- Class components already had the ability to do this kind of functionality overall
- Long-term, function components are easier for React to deal with for things like hot reloading and the upcoming Concurrent Mode. By dangling a carrot to convince folks to move to function components + hooks, it becomes easier to implement that functionality for the React team.