Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to reference an item by id in ReactJS

Getting Started with Item Referencing in ReactJS

Hello, budding programmer! Today, we'll delve into an exciting part of ReactJS: referencing an item by its ID. If you've just started coding, you might be wondering, "What does it even mean?" Think of it like this - in a huge library with thousands of books, how do you find one specific book? You use the unique ID or the ISBN, right? Similarly, in ReactJS, if you have a long list of items, you might want to select and manipulate a specific item in that list. That's when referencing an item by ID comes in handy!

The Importance of Unique IDs

Before we begin, let's talk about 'IDs' or identifiers. Each item in your list should have a unique ID - similar to the unique roll numbers in a class full of students. Having unique IDs helps us identify each item distinctly, even if there are multiple items with the same data.

Let's say, we have a list of students, and we want to select a particular student to update his or her data. Here's how we create unique IDs for each student:

const students = [
  { id: 1, name: 'John Doe', age: 20 },
  { id: 2, name: 'Jane Doe', age: 22 },
  // and so on...
];

How to Reference an Item by ID

Now that we understand the importance of unique IDs, let's take a look at how we can reference an item by its ID in ReactJS.

First, let's say we have a method that receives an ID as a parameter and needs to find the corresponding student:

findStudentById(id) {
  return students.find(student => student.id === id);
}

Here, find() is a JavaScript function that goes through all the items in the array (our students) and returns the first item that matches the condition inside the function (student.id === id).

This find function is like a librarian who checks every book until they find the one with the ISBN you gave them.

Updating an Item by ID

How about updating a student's data? Let's say we want to change John Doe's age to 21. Here's how we can do that:

updateStudentById(id, newAge) {
  students = students.map(student => {
    if(student.id === id) {
      return { ...student, age: newAge };
    } else {
      return student;
    }
  });
}

The map function here acts like a school administrator who goes through every student's record (map(student => {...}), checks if the roll number matches the one you provided (if(student.id === id)). If it matches, they update the age with the new one you provided (return { ...student, age: newAge };), else they leave the record as it is (return student;).

Deleting an Item by ID

Similarly, if we want to delete a student's data by ID, we can do it like this:

deleteStudentById(id) {
  students = students.filter(student => student.id !== id);
}

In this instance, filter is the function that behaves like a strict teacher who checks every student in the class (filter(student => {...})), and only allows those to stay who don't match the roll number you provided (student.id !== id).

Wrapping up

Picture this - you’re a mighty wizard with the power to conjure objects into existence just by uttering their names (or in our case, IDs). That's how you should feel now that you've mastered referencing items by ID in ReactJS.

Remember, just like a librarian uses the ISBN to find a book, a school administrator uses the roll number to update student data, and a strict teacher uses it to filter out a student, you can use the ID to find, update, or delete items from your list in ReactJS.

Keep practicing these methods, and soon enough, you'll be effortlessly navigating your way through your lists, making your applications more efficient and effective. So, go ahead and conjure up some magic in your ReactJS projects!