Introduced in React version 16.8, Hooks enable function components to access state and other React features, reducing the need for class components.
While Hooks generally replace class components, React has no plans to remove class components. |
Hooks enable us to “hook” into React features, including state and lifecycle methods.
Here is an example of a Hook. Don’t worry if it’s not clear yet; we’ll cover it in more detail in the next section.
import |
You need to import Hooks from React. In this example, we’re using the useState Hook to manage the application state. State typically refers to data or properties within an application that need to be monitored.
There are three rules for using Hooks:
Note: Hooks do not work in React class components. |
If you need to reuse stateful logic across multiple components, you can create custom Hooks.
We’ll explore this further in the Custom Hooks section.