Changing a list

There are two operations we can perform on a list:

  • Add item(s) to a list
  • Remove item(s) from a list

Let's take the first bullet point and make this change in the old way and then make the Redux way:

// core-concepts/list.js

// old way
let list = [1, 2, 3];
list.push(4);

// redux way
let immutablelist = [1, 2, 3];
let newList = [...immutablelist, 4];

console.log("new list", newList);
/*
[1, 2, 3, 4]
*/

The preceding code takes the old list and its items and creates a new list containing the old list plus our new member.

For our next bullet, to remove an item, we do this:

// core-concepts/list-remove.js

// old way
let list = [1, 2, 3];
let index = list.indexOf(1);
list.splice(index, 1);

// redux way
let immutableList = [1, 2, 3];
let newList = immutableList.filter(item => item !== 1);

As you can see, we produce a list not containing our item.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.190.113