What are Hooks?


The Less Technical Version

Hooks are functions in React that allow us to share code between components. Up until 2019, sharing business logic between components was not a built-in feature of React and with class-based components it was fairly cumbersome to share code. Another kind of component called a "function component" was enhanced with a feature called Hooks. This did allow us to easily share code between components. Today, any given React project with the latest version of React installed might have a mix of both class and function components together, although most React developers want to write exclusively in function components to take advantage of Hooks.

The More Technical Version

Hooks give us function composition to share business logic between components. They're functions that start with the word use like useState and useContext. They're not any different than normal functions other than we need to follow just a few rules to use them correctly and the linter that comes with React will ensure that we do. Hooks are not a way to "share state" between components, but rather they make our code more DRY by allowing common logic to be shared or even abstracted. Before Hooks, when we had class-based components, there wasn't a API in React to share code. Developers came up with patterns like Higher Order Components and Render Props which do allow us to share code but with tradeoffs that range from less-ideal to outright terrible. Since Hooks are primitive feature of React, they solve the problems we made with HoC's and Render Props.

Can we still use classes in modern React? Yes. We've always been able to mix class-components and function components (with hooks) and use them on the same project. Just consider when you write sharable code and abstractions for the entire code-base, you won't be able to write them in hooks and use them on class-based components. This might mean you'll be writing Higher Order Components and Render Props and accepting their shortcomings since you have not migrated away from classes. For these reasons, most React developers prefer to not use class components at all.

Consider hiring strategies

It's worth noting that Hooks have been released for a number of years now so we wouldn't say they're new and bleeding edge anymore. There's MANY developers who started learning React around when Hooks came out or after. Those developers don't have hardly any, if at all, experience with class-based components. Consider this as you grow your teams.

Third-party tools

Practically all modern third-party tools that you use with your project are going to be Hooks-based these days. They likely wont play nice with class-based components. Many libraries that relied on class-based components at one point have moved on to Hooks or perhaps aren't being maintained anymore. It's just one more thing to consider when adopting React. We strongly recommend you try Hooks and not classes.

i love react
© 2024 ReactTraining.com