Hooks
In this section, we are going to introduce two of the popularly used React hooks: useState
and useEffect
. These are widely used and can solve most of our problems. The useState
hook is used to initialize the state of a component and get access to a function that allows you to modify the state of that same component. The useEffect
hook, on the other hand, is used when changes are made to the component, similar to the use case for componentDidMount
or componentDidUpdate
methods in class-based components.
Note
There are other types of hooks that come bundled with the React library. You can find a complete list of these at https://packt.live/3bCTh8d.
Let's dive right into these two particular hooks in more detail.
useState
useState
is the first type of hook that we are going to use. It gives us all the functionality that this.state
and this.setState
provide for class-based components. When we call useState
, it will return an array where the first item in the...