We’ve often had to maintain components that started out simple but grew into an unmanageable mess of stateful logic and side effects. Complex components become hard to understand We’ll discuss this more in Building Your Own Hooks. This makes it easy to share Hooks among many components or with the community. Hooks allow you to reuse stateful logic without changing your component hierarchy. With Hooks, you can extract stateful logic from a component so it can be tested independently and reused. While we could filter them out in DevTools, this points to a deeper underlying problem: React needs a better primitive for sharing stateful logic.
If you look at a typical React application in React DevTools, you will likely find a “wrapper hell” of components surrounded by layers of providers, consumers, higher-order components, render props, and other abstractions.
But these patterns require you to restructure your components when you use them, which can be cumbersome and make code harder to follow. If you’ve worked with React for a while, you may be familiar with patterns like render props and higher-order components that try to solve this. React doesn’t offer a way to “attach” reusable behavior to a component (for example, connecting it to a store). It’s hard to reuse stateful logic between components Whether you’re learning React, use it daily, or even prefer a different library with a similar component model, you might recognize some of these problems. Hooks solve a wide variety of seemingly unconnected problems in React that we’ve encountered over five years of writing and maintaining tens of thousands of components. If you just want to start learning Hooks, feel free to jump directly to the next page! You can also keep reading this page to learn more about why we’re adding Hooks, and how we’re going to start using them without rewriting our applications. As we will show later, Hooks also offer a new powerful way to combine them. Instead, Hooks provide a more direct API to the React concepts you already know: props, state, context, refs, and lifecycle. Hooks don’t replace your knowledge of React concepts. You can read more about the gradual adoption strategy for Hooks in the bottom section of this page. There are no plans to remove classes from React. Hooks are now available with the release of v16.8.0. Hooks don’t contain any breaking changes. But you don’t have to learn or use Hooks right now if you don’t want to. You can try Hooks in a few components without rewriting any existing code.
Video IntroductionĪt React Conf 2018, Sophie Alpert and Dan Abramov introduced Hooks, followed by Ryan Florence demonstrating how to refactor an application to use them. React Native supports Hooks since the 0.59 release of React Native.
When upgrading, don’t forget to update all packages, including React DOM. React 16.8.0 is the first release to support Hooks.
On this page, we’ll continue by explaining why we’re adding Hooks to React and how they can help you write great applications. You can start learning Hooks on the next page. Don’t worry if it doesn’t make sense yet! This new function useState is the first “Hook” we’ll learn about, but this example is just a teaser.