No history yet

Introduction to React Hooks

What are Hooks?

In React, we build user interfaces with components. For a long time, if you wanted a component to have its own internal data, or state, you had to write it as a JavaScript class. These are called class components.

Functional components were simpler, but they were also limited. They couldn't have state or manage side effects like fetching data. They were just for displaying information.

Hooks changed all of that.

Hooks are a new addition in React 16.8 that let you use state and other React features without writing a class.

Essentially, Hooks are functions that let you “hook into” React features from functional components. You can now use state, lifecycle features, and other powerful tools without ever writing a class.

Why Use Hooks?

So, why the big shift? Class components worked, but they had some pain points that became more obvious as applications grew larger.

Hooks solve several problems:

  • Complex components become hard to understand: In class components, related logic often gets split up across different lifecycle methods (like componentDidMount and componentDidUpdate). For example, code to fetch data and code to set up a subscription might be in the same method. Hooks let you group related logic together in one place, making components cleaner and easier to read.

  • Reusing logic is difficult: React doesn't offer a great way to attach reusable behavior to a component without changing its structure. Patterns like render props and higher-order components emerged to solve this, but they often lead to deeply nested component trees, sometimes called “wrapper hell.” Hooks let you extract stateful logic from a component so it can be tested independently and reused.

  • Classes can be confusing: For both people and machines, classes are a hurdle. You have to remember to bind event handlers, and understanding how this works in JavaScript can be a common source of bugs. Functional components with Hooks are more straightforward.

A Quick Tour of Core Hooks

You'll encounter a handful of Hooks most of the time. Let's briefly look at the most common ones. We'll dive deeper into each of them later.

useState: This is the most fundamental Hook. It lets you add a state variable to your functional component. When you need your component to remember something, like the value of an input field or whether a button has been clicked, you'll use useState.

useEffect: This Hook lets you perform side effects in your components. What's a side effect? Anything that affects something outside the scope of the current function. Common examples include fetching data from an API, setting up a subscription, or manually changing the DOM.

useContext: This Hook provides a way to pass data through the component tree without having to pass props down manually at every level. It's great for sharing data that can be considered “global” for a tree of React components, such as the current authenticated user or a theme (like dark or light mode).

useReducer: An alternative to useState. It's usually preferred when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one. If you've ever used Redux, the concept will feel familiar.

useCallback and useMemo: These are performance optimization Hooks. useCallback returns a memoized version of a callback function that only changes if one of its dependencies has changed. useMemo does the same for values, returning a memoized value. They help you avoid expensive recalculations on every render.

These are the building blocks you'll use to create powerful, efficient, and easy-to-read React applications. Now, let's check your understanding of these core concepts.

Quiz Questions 1/6

What was a major problem with class components that React Hooks were designed to solve?

Quiz Questions 2/6

Which Hook is most commonly used to add a state variable to a functional component?

By now, you should have a good high-level grasp of what Hooks are and why they are such a powerful addition to React.