Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to set array in state ReactJS

Getting Started with Arrays in State ReactJS

If you're delving into the world of programming and have already dipped your toes into JavaScript and ReactJS, you're probably ready to explore more complex topics. Today, we're exploring one such concept - setting arrays in state ReactJS.

What is a State in ReactJS?

Before we dive into arrays, let's first understand what a "state" is in ReactJS. A state is similar to a personal diary that belongs to a component in ReactJS. This diary (state) contains personal details (data) about the component, and it can be updated and changed over time, just like how you update your diary with new experiences.

In technical terms, the state is a built-in object in ReactJS that allows components to create and manage their own data. So, when we talk about setting arrays in the state, we're basically talking about storing a list of data (array) in our component's personal diary (state).

Why Use Arrays in State?

Imagine owning a diary but only being able to write one type of event in it. It would be quite limiting, wouldn't it? This is where arrays come into play. They allow our diary (state) to hold a list of data, not just a single item. This makes managing and working with data in ReactJS more flexible and powerful.

Setting Up Arrays in State

Now, let's get our hands dirty and actually set up an array in a state. We'll start by creating a new component. We'll call it Diary.

class Diary extends React.Component {
  constructor() {
    super();
    this.state = {
      entries: []
    };
  }
}

In the above code, entries is our array which is currently empty. This is the equivalent of having a new, blank diary ready to be filled with experiences.

Adding Entries to our Diary

Adding data to our array is like writing in our diary. In ReactJS, we can do this using the setState method. Let's say we want to record an event, we'll call it "Experience1".

this.setState({
  entries: [...this.state.entries, "Experience1"]
});

In the above code, we're using the setState method to change our state. The [...this.state.entries, "Experience1"] part might look a bit confusing, but it's actually quite simple.

The ... is called the spread operator. It's like saying "include everything that was previously in the diary". Then, we add our new experience ("Experience1") to the end of the array.

Removing Entries From our Diary

Just as we can add experiences to our diary, we can also remove them. Let's say we want to remove "Experience1" from our diary. Here's how we can do that:

this.setState({
  entries: this.state.entries.filter(entry => entry !== "Experience1")
});

The filter method works by looking at each entry in our diary and comparing it with "Experience1". If it matches, it gets removed.

Recap and Conclusion

So, what have we learned? We've learned that the state in ReactJS is like a personal diary for our components. We can store different types of data in this diary, including arrays. We've also learned how to add and remove items from our array using the setState method.

Programming can often feel like a complex and intricate dance, but by breaking it down and imagining it as something more familiar (like a diary), it can become easier to understand.

As we close this entry, keep in mind that these concepts are the building blocks to creating dynamic and interactive web applications. So keep practicing, keep experimenting, and remember, every expert was once a beginner. Happy coding!