react - ReactJS Component Life Cycle - react js - reactjs
Lifecycle Methods
- The component Life cycle has different Phases:
- Mounting
- Updating
- Unmounting
Mounting
- Mounting means putting elements into the DOM.
- React has four built-in methods that gets called, in this order, when mounting a component:
- constructor()
- getDerivedStateFromProps()
- render()
- componentDidMount()
constructor()
- constructor() method is called before anything else, when the component is initiated, and it is place to set up the original state and other initial values.
- The constructor() method is called with the props, as arguments, and you must always start by calling the super(props) before anything else, this will initiate the parent's constructor method and allows the component to receive methods from its parent (React.Component).
Sample Code
Output
My Bike Color is Yellow
getDerivedStateFromProps()
- The getDerivedStateFromProps() method is called right before rendering the element(s) in the DOM.
- This is the natural place to set the state object based on the initial props.
- It takes state as an argument, and returns an object with changes to the state.
- The example below starts with the favorite color being "green", but the getDerivedStateFromProps() method updates the favorite color based on the favcol attribute:
Sample Code
Output
My Car Color is blue
render()
- The render() method is required, and is the method that really outputs the HTML to the DOM.
Sample Code
Output
Welcome to Wikitechy Tutorials
componentDidMount
- The componentDidMount() method is called after the component is rendered.
- This is where you run statements that requires that the component is already placed in the DOM.
Sample Code
Output
My Drees Color is Green
Updating
- The next stage in the lifecycle is when a component is updated.
- A component is updated whenever there is a change in the component's state or props.
- React has built-in methods that gets called, in this order, when a component is updated:
- getDerivedStateFromProps()
- shouldComponentUpdate()
- render()
- getSnapshotBeforeUpdate()
- componentDidUpdate()
- The render() method is required and will always be called, the others are optional and will be called if you define them.
getDerivedStateFromProps
- getDerivedStateFromProps is the first method that is called when a component gets updated.
- This is still the place to set the state object based on the initial props.
- The example below has a button that changes the course to java, but since the getDerivedStateFromProps() method is called, which updates the state with the course from the course attribute, the course is still rendered as react.
Sample Code
Output
shouldComponentUpdate
- shouldComponentUpdate() method you can return a Boolean value that requires whether React should continue with the rendering or not.
- The default value is true.
The example below shows what happens when the shouldComponentUpdate() method returns false:
Sample Code
Output
Here the same example as above, but this time the shouldComponentUpdate() method returns true instead:
Sample Code
Output
render
- The render() method is of course called when a component gets updated, it has to re-render the HTML to the DOM, with the new changes.
- The example below has a button that changes the course to python:
Sample Code
Output
If you are clicking the Change Course button, the react course will changed to python course.
getSnapshotBeforeUpdate
- getSnapshotBeforeUpdate() method you have access to the props and state before the update, meaning that even after the update, you can check what the values were before the update.
- If the getSnapshotBeforeUpdate() method is present, you must include the componentDidUpdate() method, otherwise you will get an error.
- The example below might seem complex, but all it does is this:
- When the component is mounting it is rendered with the course "react".
- When the component has been mounted, a timer changes the state, and after one second, the course becomes "python".
- This action triggers the update phase, and since this component has a getSnapshotBeforeUpdate() method, this method is executed, and writes a message to the empty DIV1 element.
- Then the componentDidUpdate() method is executed and writes a message in the empty DIV2 element:
Sample Code
Output
My Course is Python Before the update, the course was react Now the updated course is python
componentDidUpdate
- The componentDidUpdate method is called after the component is updated in the DOM.
- The example below might seem complex, but all it does is this:
- When the component is mounting it is rendered with the course "react".
- When the component has been mounted, a timer changes the state, and the course becomes "java".
- This action triggers the update phase, and since this component has a componentDidUpdate method, this method is executed and writes a message in the empty DIV element:
Sample Code
Output
My Best Course is java Now The updated course is java
Unmounting
- when a component is removed from the DOM, or unmounting as React likes to call it.
- React has only one built-in method that gets called when a component is unmounted:
- componentWillUnmount()
componentWillUnmount
- The componentWillUnmount method is called when the component is removed from the DOM.
Sample Code
Output
If you are clicking the Delete header button to display an alert box.