useEffect Hook
Learn how the “useEffect” hook replaces traditional lifecycle methods in React. This lesson will go through its use case, syntax, and usage. Later in the chapter, practice exercises are provided.
We'll cover the following...
What is it?
The useEffect
hook provides a way to manage side effects in a React functional component. Think of the useEffect
hook as a partial replacement for React lifecycle events. With the useEffect
hook, it is possible to replicate behavior for componentDidMount
, componentDidUpdate
and componentWillUnmount
methods.
In other words, you can react upon changes happening in a component that is using the useEffect
hook. How great is that 🎉?
When do we use it?
Some common scenarios to use the useEffect
hook with are mentioned below:
- Add an event listener for a button
- Fetch data from API