Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to sHow history of changes in ReactJS

Understanding the Need for Tracking Changes

Before we dive into the how, let's understand the why. Imagine you are painting a canvas. Each stroke you make is a change. Now, what if you want to undo a stroke? If you remember the previous state of your canvas, you can easily revert. The history of changes is just like the memory of your canvas. In ReactJS, the 'canvas' is the state of your application, and each 'stroke' is a state change.

In programming, tracking changes is essential for debugging and understanding how your application evolves over time. So, today we'll be exploring how to show the history of changes in ReactJS.

A Brief Introduction to State and Props

In ReactJS, 'state' and 'props' are the two main players that control the data flow. State refers to the data that can change over time, while props (short for properties) are the values that are passed down from the parent component to the child component.

Think of state as the brain of your component. It holds the up-to-date values of the variables that can change during the application's lifecycle. On the other hand, props are like instructions given to a component, telling it what to display.

Implementing History of Changes

Now, let's get to the fun part. We'll create a simple counter application in ReactJS, where we can increment or decrement a number and track the history of changes.

First, let's create our state and a way to change it:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <h2>Count: {count}</h2>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={() => setCount(count - 1)}>Decrement</button>
    </div>
  );
}

export default Counter;

In this code, useState is a React hook that allows us to add React state to our function component. We're setting the initial state to 0 and creating a setCount function that we can use to update the state.

Next, let's add a way to track the history of changes:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);
  const [history, setHistory] = useState([0]);

  const handleIncrement = () => {
    const newCount = count + 1;
    setCount(newCount);
    setHistory([...history, newCount]);
  };

  const handleDecrement = () => {
    const newCount = count - 1;
    setCount(newCount);
    setHistory([...history, newCount]);
  };

  return (
    <div>
      <h2>Count: {count}</h2>
      <button onClick={handleIncrement}>Increment</button>
      <button onClick={handleDecrement}>Decrement</button>
      <h2>History</h2>
      <ul>
        {history.map((item, index) => (
          <li key={index}>{item}</li>
        ))}
      </ul>
    </div>
  );
}

export default Counter;

In the above code, we created a new state variable history and updated it every time we change the count using the setHistory function. The spread operator (...) is used to copy the existing history into a new array and then add the newCount to it.

Taking a Step Back

The way we have implemented history here is akin to leaving breadcrumbs along a path. Each time we change the state, we leave a breadcrumb (or, in this case, we append the new state to the history array). This way, we can always look back to see the path we've taken.

Conclusion

In a nutshell, tracking changes in ReactJS is like keeping a diary of your component's life. It allows you to revisit the past and understand the journey of your application's state over time.

In this post, we've seen how to implement this using the built-in hooks of ReactJS. This is a simple implementation, but it illustrates the concept well. As you continue your journey as a programmer, you'll find more complex scenarios where tracking history will be beneficial.

Just remember, like the artist knowing each stroke on their canvas or the writer knowing each word they've written, understanding the changes in your application will make you a better programmer. So keep coding, keep tracking, and keep evolving!