Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to write an array in ReactJS

Understanding Arrays in JavaScript

Before we dive into writing an array in ReactJS, let's briefly cover what arrays are in JavaScript. You can think of an array as a big box with many compartments. Each compartment can hold different things like numbers, strings, or even other arrays! These compartments are called "elements" in an array.

For instance, let's take an array of fruits.

let fruits = ["apple", "banana", "cherry"];

In this array, "apple", "banana", and "cherry" are the elements contained within the array.

Creating Arrays in ReactJS

Now that we understand what an array is, let's create one in ReactJS. In ReactJS, we usually use arrays to render a list of things. For example, if we have a list of fruits, we might want to display each fruit on the web page in an organized manner.

class Fruits extends React.Component {
  constructor() {
    super();
    this.state = {
      fruits: ["apple", "banana", "cherry"]
    };
  }

  render() {
    return (
      <div>
        {this.state.fruits.map(fruit => <p>{fruit}</p>)}
      </div>
    );
  }
}

In this code, this.state.fruits.map(fruit => <p>{fruit}</p>) is taking each element in the fruits array and returning a paragraph <p> with the fruit name. This is similar to going through each compartment of our box (the array) and showing what's inside each one.

Manipulating Arrays in ReactJS

Just as we can add or remove things from a box, we can also add or remove elements from an array. Let's say we want to add a fruit to our list. We can do so using the setState method in ReactJS.

this.setState(prevState => ({
  fruits: [...prevState.fruits, 'orange']
}));

This code says take the previous state of the fruits array, and add 'orange' to it. It's like going back to our box of fruits and putting an orange in an empty compartment.

Similarly, we can remove an element from our array using the filter function.

this.setState(prevState => ({
  fruits: prevState.fruits.filter(fruit => fruit !== 'banana')
}));

This code filters out 'banana' from the fruits array. It's like going to our box of fruits and taking out the banana.

Reacting to Changes in Arrays

ReactJS makes it easy to update the user interface when the state of an array changes. Whenever we call setState, ReactJS automatically updates the components that depend on that state. For example, if we add or remove a fruit from our array, ReactJS automatically updates the list of fruits displayed on the web page.

Wrapping Up

Understanding arrays and how to manipulate them in ReactJS is like learning to organize your toys in a box. You start with an empty box (an empty array), and as you play (program), you add toys (elements) to the box. You might also decide to take out some toys (remove elements) later. And the best part is, mom (ReactJS) automatically cleans up (updates the user interface) whenever you change the contents of the box (call setState).

Just like playing with toys, programming with arrays in ReactJS is a lot of fun. You can create all sorts of interesting things by adding, removing, or changing the elements in an array. So keep experimenting and happy coding!