Lab: Component LifeCycle
Last updated
Last updated
Much like other things you've dealt with you may want to do certain actions based on events in the "life" of a React component. Perhaps you want to do something after a component is loaded for the first time or after it updates.
Think about things like Sequelize hooks, JavaScript classes, or DOM events. For example, Sequelize's beforeCreate
hook allows you to run some code before a new entry goes into your SQL table. In the same way, React components allow you to tap into their own lifecycle methods. They sometimes have long, intimidating sounding names, which you'll have to get used to!
React components have 3 overarching categories that the lifecycle methods fit into. Some lifecycle events may overlap multiple categories. These categories are Mounting
, Updating
, and Unmounting
. Sometimes you will see Mounting
broken out into Initialization
and Mounting
as it is in this medium article.
This is the birth of a React component. Any lifecycle event that takes place around the component coming into existence fits here:
constructor
componentDidMount
render
This is basically any change or event that happens to the component that doesn't have to do with its birth or death:
getDerivedStateFromProps
shouldComponentUpdate
getSnapShotBeforeUpdate
componentDidUpdate
render
This is when your component is about to shuffle off its mortal coil. When it passes to the code beyond and pushes up binary daisies. Only one lifecycle method is generally used here:
componentWillUnmount
The reason you might use this is to clean up any open connections or free any memory in use. In object-oriented languages this is normally done in a destructor
method - i.e., the opposite of the constructor
.
Image credit to programmingwithmosh.com
Note that these methods may be deprecated. If so, why was it deprecated and what replaced it?
Your presentation doesn't have to be long, but it should minimally include:
A definition
Which part of the 3 parts of the lifecycle does it occur in
A code example or an explanation of a scenario when the lifecycle method would be used
constructor
componentDidMount
getDerivedStateFromProps
shouldComponentUpdate
getSnapShotBeforeUpdate
componentDidUpdate
render
componentWillUnmount
componentWillReceiveProps (deprecated - here's why)