Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to update array state in ReactJS

Understanding State in ReactJS

Before we dive into the specifics of how to update an array state in ReactJS, let's first understand what "state" is in a ReactJS context. The "state" in ReactJS is a built-in object that is used to contain data or information about a component. You can think of it as a memory store for the component, allowing it to remember data and make use of it.

For example, let's imagine a simple counter application where you click a button, and the number goes up. The current number that is displayed would be held in the state. Each time you click the button, the state's value would be updated to reflect the new number.

An Array State

An array state, on the other hand, is a state that holds an array. An array is a special type of data structure in JavaScript that can hold multiple values at the same time. It's like a list of items, and each item can be accessed by its position in the list.

Let's say you have a state that holds a list of to-do items. Each time you add a new to-do item, you would need to update the array state to include the new item.

Setting Up Array State

Before we can update an array state, we first need to create it. To create an array state in ReactJS, we use the useState function. The useState function is a Hook that lets you add React state to function components.

Here's an example of how to create an array state:

import React, { useState } from 'react';

function TodoList() {
  const [todos, setTodos] = useState([]);
}

In this code snippet, useState([]) initializes our state with an empty array. The useState function returns a pair: the current state value (todos) and a function that lets you update it (setTodos).

Updating the Array State

Now comes the crux of the matter - updating the array state. The key thing to remember when updating state in React is to always use the provided updater function (setTodos in our case).

Why? Because state updates may be asynchronous, and directly modifying the state may not render the component with the new data.

Consider our to-do list example. To add a new to-do item, we'll use the setTodos function, passing in a new array that includes the existing to-do items and the new one.

Here's how to do that:

function addTodo(newTodo) {
  setTodos([...todos, newTodo]);
}

In this code, [...todos, newTodo] creates a new array that contains all of the items in the todos array (using the spread operator ...), followed by the new to-do item newTodo.

Removing an Item from the Array State

But what if we want to remove an item from our list? JavaScript provides a handy method called filter for arrays which we can use:

function removeTodo(todoToRemove) {
  setTodos(todos.filter(todo => todo !== todoToRemove));
}

The filter function returns a new array that includes only the items for which the provided function (todo => todo !== todoToRemove) returns true. In this case, it includes only the items that are not equal to todoToRemove.

Final Thoughts

Updating an array state in ReactJS might seem a bit intimidating at first, especially if you're new to web development. But once you take the time to understand what's going on, it becomes a lot less daunting.

Just remember, the state in ReactJS is like a component's memory. When you're updating the state, you're just updating what the component remembers. And when you're dealing with an array state, you're dealing with a list that the component remembers.

So, go ahead and give it a try. Experiment, break things, learn, and most importantly, have fun with it. Happy coding!