What is a Hook in React?
A hook is a special function provided by React that lets you use React core features—state and component lifecycle methods—within a function component. While state is an in-built object in React that adds interactivity and dynamic mechanism to components, the lifecycle tracks the phases components go through, from their initialization to their eventual demise (when a user navigates away or exits from an application UI).
There are three major cyclic phases React components go through, as explained in Chapter 2, Getting Started with React: mounting, updating, and unmounting. Each of these phases has what we call lifecycle methods that can be used during the rendering of React components.
We observed the presence of certain methods, such as componentWillMount()
, componentDidMount()
, componentWillUpdate()
, and componentDidUpdate()
, during the class component’s lifecycle. React hooks are used to make function components stateful without...