By WalkingTree   September 23, 2020

React Hooks instead of Classes

React Hooks has been around for quite a while but yet not many developers are using it. Mainly because they are already working on a large application that is difficult to move or they are more comfortable in using classes. Let’s take a look into reasons why should go for React Hooks instead of classes. 

Refactor a functional component into a class component – Changing from a functional component to a class component requires refactoring, depending on how complex the component is. With React Hooks, functional components have the capability of tapping into the state, the refactoring effort will be minimal. 

You don’t have to worry about ‘this’ – If you are familiar with JavaScript, you know that ‘this’ in JavaScript doesn’t work the same in other languages. When it comes to React Hooks, you don’t have to worry about this at all. This is good for beginners as well as experienced developers. 

Easier to decouple logic from UI – With React hooks, logic and UI is easier to separate. No need for HOC or render props. Hooks do it elegantly with less boilerplate and more intuitive compositions of UI.

Sharing stateful logic between components – With the class-based approach, it is hard to share stateful logic between various components. Consider two components where both have to fetch, sort, and display data from two different data sources. Even though both components have the same functionality, it is hard to share the logic. With Custom React Hooks you can extract these reusable stateful logics and test them separately. 

Read on to know what are the other reasons for choosing React Hooks.

Blogs

Leave a comment

Translate »