Book Image

Mastering Immutable.js

By : Adam Boduch
Book Image

Mastering Immutable.js

By: Adam Boduch

Overview of this book

Immutable.js is a JavaScript library that will improve the robustness and dependability of your larger JavaScript projects. All aspects of the Immutable.js framework are covered in this book, and common JavaScript situations are examined in a hands-on way so that you gain practical experience using Immutable.js that you can apply across your own JavaScript projects. The key to building robust JavaScript applications using immutability is to control how data flows through your application, and how the side-effects of these flows are managed. Many problems that are difficult to pinpoint in large codebases stem from data that’s been mutated where it shouldn’t have been. With immutable data, you rule out an entire class of bugs. Mastering Immutable.js takes a practical, hands-on approach throughout, and shows you the ins and outs of the Immutable.js framework so that you can confidently build successful and dependable JavaScript projects.
Table of Contents (23 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Changing collection values


Once you've added data to your collection, you'll probably need to change it. Lists and maps each have two approaches to change existing values.

Changing list values

Lists can either set a value or update a value. The distinction is subtle, so let's compare the two approaches now.

Setting list values

When you set list values using the set() method, you're changing an existing value. More specifically, you're overwriting the current value at a given index with a new value:

const myList = List.of(1);
const myChangedList = myList.set(0, 2);

console.log('myList', myList.toJS());
// -> myList [ 1 ]
console.log('myChangedList', myChangedList.toJS());
// -> myChangedList [ 2 ]

You're updating the first list value—because the index you're passing to set() is 0—with a value of 2. Using set() like this is a good choice when you know ahead of time what the new value should be. But what about when the new value depends on the current value?

Updating list values

When the new...