Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get selected value in a list in ReactJS

Getting Started

Imagine you're in a grocery store and you have a shopping list. You're going through the aisles, picking out items from your list, and putting them in your cart. Now, think of ReactJS as the shopping list and the selected value in a list as the item you're picking off the shelf. Just like how you'd reach out, grab an item, and put it in your cart, ReactJS gives us a way to "grab" a value from a list and use it in our code. Let's explore how to do this.

Understanding Lists in ReactJS

A list in ReactJS is like an array in JavaScript. It's a collection of items, and each item can be of any type - a string, a number, an object, or even a function. ReactJS uses a function called map() to loop through a list and generate a new array.

Here is an example of a list in ReactJS:

const groceries = ['Apples', 'Oranges', 'Bananas'];

To display this list in a React component, you would use the map() function, like so:

const GroceryList = () => {
  const groceries = ['Apples', 'Oranges', 'Bananas'];

  return (
    <ul>
      {groceries.map((item, index) => (
        <li key={index}>{item}</li>
      ))}
    </ul>
  );
};

Selecting a Value from a List

Now, let's say you want to be able to select an item from this list and do something with it. In ReactJS, you would typically do this using state.

State in ReactJS is like a container that holds data. It can be changed and when it does, the component that it's in re-renders.

In this case, you might have a piece of state called selectedItem, that you set whenever you click on an item in the list.

const GroceryList = () => {
  const groceries = ['Apples', 'Oranges', 'Bananas'];
  const [selectedItem, setSelectedItem] = React.useState(null);

  return (
    <ul>
      {groceries.map((item, index) => (
        <li key={index} onClick={() => setSelectedItem(item)}>
          {item}
        </li>
      ))}

      <p>Selected Item: {selectedItem}</p>
    </ul>
  );
};

In this example, whenever you click on a list item, the setSelectedItem function is called with the item that was clicked. This sets the selectedItem state to be the item that was clicked, and because state has changed, the component re-renders, showing the new selected item.

Under the Hood

Let's break down what's happening here. When you click on an item, the onClick event handler fires. This is like your hand reaching out to grab an item off the shelf. The setSelectedItem function is called with the item you clicked on. This is like putting the item in your cart.

The selectedItem state is then updated with the item you clicked on. This is like marking off the item on your shopping list. And finally, because state has changed, the component re-renders, showing the new selected item. This is like looking in your cart and seeing the item there.

Conclusion

Just like the satisfaction you feel when you've found all the items on your shopping list, understanding how to select a value from a list in ReactJS can bring a sense of accomplishment. It's a fundamental aspect of ReactJS that opens the door to a multitude of functionalities in your application.

It's all about reaching out, selecting an item, and putting it in your cart. With this guide, you've not only learned how to select an item from a list in ReactJS, but you've also learned the importance of state, and how it drives the re-rendering process in a React component. So the next time you're grocery shopping, remember, you're not just picking out items, you're practicing ReactJS! Happy coding!