Skip to main content

Lifecycle of React Native Component [2020 Edition]

What are the life cycle methods of React Native Component?

A component's life cycle in React Native can be divided into 4 phases:

react native component life cycle methods
React Native Component life cycle phases

  1. Mounting: In this phase,  component instance is created and inserted into the DOM.
  2. Updating: In updating phase, a react component is said to be born and it start growing by receiving new updates.
  3. Unmounting: In this phase, a react component gets removed from actual DOM.
  4. Error Handling: It is called when any error occurs while rendering the component.
Now let's discuss about different methods that gets called during these phases.

Mounting phase

Below are the methods which gets called when instance of component is created and inserted into the DOM.

  • Constructor()
  • static getDerivedStateFromProps()
  • render()
  • ComponentDidMount()

Constructor()

constructor(props) {
  super(props);
  this.state = { employeeId: 0 };
}
It is first method which gets called in the lifecycle of react native component. It is used to initialise the component with initial state. In constructor no UI rendering is done. It receives props as an argument and we can set state in this method.

static getDerivedStateFromProps()

static getDerivedStateFromProps(props, state)
It is called just before the render method, both on the initial mount and the subsequent update. It is the replacement of componentWillReceiveProps method. We cannot update state inside this method. If we want to update the state then we should return and object else return null.

render()

render(){
    return(
      <View>
            <Text>Hello React Native</Text>
      </View>
 );
};
It is the only required method in react component. It tells what to display on the screen. render() method is pure function which means it doesn't modify the state. It returns the same result each time it is invoked and it does not directly interact with the browser.

ComponentDidMount()

 componentDidMount(){
    this.getData();
  }
This method gets called when react native component has finished the rendering part. It is good place to load data from remote endpoint and update the state with the result. This will refresh the UI of our mobile screen. Whenever we make any change in the state then render() methods gets called which will ultimately reflect the changes on the screen.

Updating phase

Whenever there is any change in the state or props then updating process of the component get started. Below are the methods which gets called in the updating phase of react component.

static getDerivedStateFromProps()

static getDerivedStateFromProps(props, state)
As we already discussed above, this method gets called whenever any changes occurred in state or props.

shouldComponentUpdate()

shouldComponentUpdate(nextProps, nextState)
it is called before rendering when new props or states are being received. Default value of this method is true.  This method doesn't get called on initial render or when forceUpdate is used. If we want to stop re-rendering on changing state or props then we can return false in shouldComponentUpdate() method.

render()

Whenever there is any change in the state or props then render methods gets called again.

getSnapshotBeforeUpdate()

getSnapshotBeforeUpdate(prevProps, prevState)
This method gets called right before the most recently rendered output is committed. It enables our component to  some information from the DOM  before it is potentially changed.

componentDidUpdate()

componentDidUpdate(prevProps, prevState, snapshot)
componentDidUpdate() method is invoked immediately after re-rendering of the component gets completed. It is important to note here that it is not called for the initial render.

Unmounting phase

componentWillUnmount()

componentWillUnmount() {
   // Destroy any data before component is removed
 }
componentWillUnmount() method gets invoked when component is removed from the DOM. We can perform clean up tasks in this method like invalidating timers, cancelling ongoing network request.

Error handling phase

Whenever any error occurred while rendering the component then error handling phase of react gets called with below methods.
  • static getDerivedStateFromError()
  • componentDidCatch()

static getDerivedStateFromError()

static getDerivedStateFromError(error) {
    return { isError: true };
  }
It is called whenever any error occurred while rendering.It receives the error as a parameter and returns the value to update the state.

componentDidCatch()

  componentDidCatch(error, info) {
}
This is invoked when any error is thrown by descendant component. It receives two parameter 
  1. error: The error that was thrown
  2. info: It is an object having information about which component threw that error.

If you enjoyed reading this article 😀, then please don't forget to share it with your friends and do subscribe this blog to receive more technical posts in future via email.

Comments

  1. Thanks for giving the information. This blog is very helpful. I learned a lot from this I hope you will keep sharing such information in the future also.
    visit site

    ReplyDelete
  2. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site. For more info:- React native App Development

    ReplyDelete

  3. This post is so usefull and informative.keep updating with more information...
    IOS Language
    Latest Version Of Swift

    ReplyDelete
  4. nice post
    please keep sharing more posts like this..............Facebook Bellen

    ReplyDelete

Post a Comment

Popular posts from this blog

SOLID Design Principles in Swift

What are solid principles in programming world? 5 SOLID design principles in swift SOLID is acronym used in software programming for making software design more understandable, flexible, scalable and maintainable.  Every Software Developer must be aware of 5 SOLID principles in order to deliver good quality code SOLID stands for what? S -  Single Responsibility principle O - Open Closed Principle L - Liskov substitution Principle I - Interface segregation Principle D - Dependency Inversion Principle If we apply 5 SOLID principles while creating iOS/Mac Apps then the benefits which we will get are as follow: ·       We will have flexible code which can be changed easily. ·       Software code becomes more reusable. ·       Software developed will be robust, scalable and stable. ·       Code is loosely couple which means dependency between the elements is low. Now let’s discuss each principle one by one. 1.)  Single Responsibility Principl

What's new in iOS 13

Hi guys, Lets have a quick review on what new features Apple has released in its latest iOS 13 version. iOS 13 version iOS 13 makes old iPhone faster, last longer  - In iOS 13, Face ID unlocking will be 30% faster than before , app launch times are two times faster. Apple also found a way to make app downloads smaller, up to 60% on average. iOS 13 Dark Mode  - Apple introduced new dark mode option which changes the entire look of the operating system from light to dark. All native Apple apps feature Dark Mode support, and third-party apps can use Dark Mode APIs to add Dark Mode integration. iOS 13 features a QuickPath keyboard  - With iOS 13, Apple's default QuickType keyboard will be incorporating swipe-to-type, a popular way of sliding across the keyboard to form words. Before this, we have to use extensions like Google’s Gboard and SwiftKey Sign - In with Apple - Now, we can use Apple sign In to  conveniently sign in to third-party accounts.Sign-in with A