Reputation: 1244
Particularly speaking about state I've been playing around with hooks and honestly I don't see many benefits comparing to class components. It is said that a benefit is that you can add state to a function component when required in the future but to me transforming into a class is not a big deal.
Upvotes: 4
Views: 114
Reputation: 435
Here are some of the benefits using hooks, taken from the React's documentation.
- Hooks allow you to reuse stateful logic without changing your component hierarchy. This makes it easy to share Hooks among many components or with the community.
With Hooks you can reuse your API calls and make your code more usable, which leads to less time spent on rewriting your expected behaviour.
- Hooks let you split one component into smaller functions based on what pieces are related (such as setting up a subscription or fetching data), rather than forcing a split based on lifecycle methods. You may also opt into managing the component’s local state with a reducer to make it more predictable.
This makes your components more compact, which in turn makes your code more readable, which is what you would want to see in your everyday code.
- Classes confuse both people and machines
In javascript this
is not interpreted as the same as other languages and also whether to use classes or functional components confuse developers, why would you need to choose one if you can do everything functionally? Also for performance issues when folding the components classes are not minimized as well as functions and for React to stay relevant it should be optimized.
Upvotes: 1
Reputation: 372
Why Hooks ?
Without hooks you can’t reuse stateful logic between components
Life cycles are weird
Classes are not cool
Upvotes: 4