What is getDerivedStateFromProps( props,state) as a lifecycle in Reactjs ?

Farihatul Maria
3 min readMar 26, 2023
What is getDerivedStateFromProps( props,state) as a lifecycle in Reactjs ?

When it comes to building complex web applications, ReactJS has been a popular choice among developers due to its ease of use and flexibility. One of the key features of ReactJS is its lifecycle methods, which allow developers to perform certain actions at different stages of a component’s lifecycle. In this article, we’ll be focusing on the getDerivedStateFromProps() lifecycle method and how it can be used to optimize the performance of your ReactJS applications.

Understanding the Component Lifecycle in ReactJS

Before we dive into the details of getDerivedStateFromProps(), it's important to understand the basic component lifecycle in ReactJS. A ReactJS component can go through several stages during its lifecycle, from being mounted on the DOM to being unmounted. Each stage in the lifecycle is associated with a set of methods that allow developers to perform certain actions.

The following are the basic stages in the lifecycle of a ReactJS component:

  • Mounting: This is the stage when a component is created and inserted into the DOM. The constructor(), static getDerivedStateFromProps(), render(), and componentDidMount() methods are called during this stage.
  • Updating: This stage is triggered whenever there is a change in the component’s state or props. The static getDerivedStateFromProps(), shouldComponentUpdate(), render(), getSnapshotBeforeUpdate(), and componentDidUpdate() methods are called during this stage.
  • Unmounting: This is the stage when a component is removed from the DOM. The componentWillUnmount() method is called during this stage.

What is getDerivedStateFromProps()?

getDerivedStateFromProps() is a static method that is called whenever there is a change in the component's props. This method takes the props and the current state of the component as arguments and returns a new state object. The new state object is then used to update the component's state.

The purpose of getDerivedStateFromProps() is to allow developers to update the component's state based on changes in the props. This is particularly useful in situations where the state of a component is dependent on the props. By using getDerivedStateFromProps(), developers can ensure that the component's state is always in sync with the props.

Example Usage of getDerivedStateFromProps()

Let’s take a look at an example of how getDerivedStateFromProps() can be used in a ReactJS component. In this example, we have a component that displays a list of products. The component takes a prop called products that contains an array of product objects. Each product object has a name and price property.

class ProductList extends React.Component {
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.products !== prevState.products) {
return { products: nextProps.products };
}
return null;
}

constructor(props) {
super(props);
this.state = {
products: props.products,
};
}

render() {
const { products } = this.state;
return (
<div>
{products.map((product) => (
<div key={product.name}>
<span>{product.name}</span>
<span>{product.price}</span>
</div>
))}
</div>
);
}
}

In the above code, we have defined a ProductList component that takes a products prop. The constructor() method initializes the state of the component with the products prop.

The getDerivedStateFromProps() method is used to update the state of the component whenever the `products

--

--