Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get old props in component update in ReactJS

Introduction

As you venture into the world of programming, it's important to understand that learning is a continuous process. Today, we will be delving into ReactJS, a popular JavaScript library used for building user interfaces. Specifically, I will guide you on how to get old props in a component update in ReactJS. Don't worry if this sounds a little complex right now. By the end of this blog, you will not only understand what it means but also know how to implement it.

Understanding Props and Components in ReactJS

Before we dive into getting old props in a component update, it's important to clarify what "props" and "components" are in the context of ReactJS.

What are Components?

Think of components as the building blocks of any ReactJS application. Just like a building is constructed using bricks, a ReactJS application is built using components. Each component has a specific task or function in the overall application.

Here's an example of a simple ReactJS component:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

In the example above, Welcome is a component that takes in props as an argument and returns a greeting message.

What are Props?

Props, short for properties, are how components talk to each other in ReactJS. If components were people, props would be the language they speak.

In the Welcome component above, props is an object that has a property name. When we use the Welcome component, we can pass in a name like this:

<Welcome name="Sara" />

The Lifecycle of ReactJS Components

To understand how to get old props during a component update, we need to understand the lifecycle of ReactJS components.

The lifecycle of a component can be thought of as the series of events that happen from the birth of a component (when it's first rendered) to its death (when it's removed from the user interface).

ReactJS provides several lifecycle methods that you can override to run code at particular times in the process. For getting old props, the relevant lifecycle method is componentDidUpdate.

The componentDidUpdate Lifecycle Method

componentDidUpdate is invoked immediately after updating occurs. This method is not called for the initial render. Here's an example of how it's used:

componentDidUpdate(prevProps) {
  // Typical usage (don't forget to compare props):
  if (this.props.userID !== prevProps.userID) {
    this.fetchData(this.props.userID);
  }
}

In the example above, componentDidUpdate receives prevProps as its first argument. prevProps are the props that the component had before it was updated.

Getting Old Props in Component Update

Now that we understand componentDidUpdate and its prevProps parameter, getting old props is straightforward. The old props are simply prevProps in componentDidUpdate.

Here's an example:

componentDidUpdate(prevProps) {
  if (prevProps.name !== this.props.name) {
    console.log('Name changed from ', prevProps.name, ' to ', this.props.name);
  }
}

In the example above, we're logging a message whenever the name prop changes. prevProps.name is the old name, and this.props.name is the new name.

Conclusion

I hope this blog post has helped you understand how to get old props in a component update in ReactJS. It can seem a bit tricky at first, but once you understand the lifecycle of ReactJS components and the componentDidUpdate method, it becomes a lot easier.

Happy coding!