From the course: Learning Full-Stack JavaScript Development: MongoDB, Node, and React
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Component life-cycle and side effects
From the course: Learning Full-Stack JavaScript Development: MongoDB, Node, and React
Component life-cycle and side effects
- [Instructor] Every React component has a life cycle. First it gets rendered by React in memory then React mount the component into the DOM, then the component might be updated again in the DOM and it can also be removed from the DOM. React provides method for us to execute code around these events in the life cycle of a component. We can for example, do something after the component is rendered or right before it gets re-rendered or unmounted. The simplest method to do that in React is with a useEffect function. Just like useState, useEffect is a function to define a side effect for the component. The use something functions in React are known as hooks. They were introduced a few years ago as an alternative to the other method that we used before them, which is to use React class components. And React still supports class components but using React hooks in function components is cleaner in my opinion, and I use it…