Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get one value form state array at a time in ReactJS

Understanding State in ReactJS

Before we dive into how to get one value from a state array at a time in ReactJS, let's understand what state is. In plain English, the state in ReactJS is like a data store for components. It's as if each component is given its own little memory space, where it can keep things it needs to remember.

For example, say you're designing a component for a game. The state could store the player's current score, the number of lives left, and so on. It's like a little backpack the component carries around, storing the items it needs to function.

Now, let's imagine that the backpack(state) is not just carrying one item but an array of items. How would we go about retrieving one item at a time? That's exactly what we are going to discuss in this blog.

Creating State Array in ReactJS

Before we learn how to retrieve one value at a time from a state array, let's first learn how to create a state array. ReactJS uses a function called useState to create and manage state. Here's how you can use it to create a state array:

import React, { useState } from 'react';

function App() {
  const [items, setItems] = useState(['Apple', 'Banana', 'Cherry']);

  return (
    <div className="App">
      <h1>My Fruits</h1>
    </div>
  );
}

export default App;

In the above code, we've created a state variable items that's initialized with an array of fruits. The setItems is a function that we use to change the value of items.

Retrieving One Value at a Time From State Array

Now let's say we want to show only one fruit at a time and have a button that will allow us to cycle through the fruits. For this, we'll need another state variable to keep track of which fruit we're currently showing. This is how you'll do it:

import React, { useState } from 'react';

function App() {
  const [items, setItems] = useState(['Apple', 'Banana', 'Cherry']);
  const [currentIndex, setCurrentIndex] = useState(0);

  return (
    <div className="App">
      <h1>My Fruits</h1>
      <p>Current fruit: {items[currentIndex]}</p>
      <button onClick={() => setCurrentIndex((currentIndex + 1) % items.length)}>
        Next Fruit
      </button>
    </div>
  );
}

export default App;

In the code above, currentIndex is a state variable that we use to keep track of the current fruit. Initially, it's 0, so the first fruit "Apple" is shown. When you click the "Next Fruit" button, currentIndex is incremented by 1, and the next fruit "Banana" is shown. If currentIndex exceeds the length of items, it resets to 0, thanks to the modulus (%) operator. This allows us to cycle through the fruits continuously.

Conclusion

To wrap up, ReactJS state is a powerful tool. It's like a personal memory space for each component, holding all the necessary data it needs to function correctly. When dealing with an array of data, we can smartly use another state variable to keep track of the current index, thus enabling us to retrieve one value at a time. This approach is like having a magic pointer in our backpack, always pointing to the item we need.

Remember, understanding these core concepts and getting comfortable with them is like learning to play with building blocks. You can then use these blocks to build complex, efficient, and beautiful structures (or in our case, web applications). So, keep practicing and happy coding!