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

Transformations versus mutations


Transformation methods and mutative methods can be a source of confusion when it comes to change detection. Transformation methods are used to provide a side-effect with the data that it needs to detect changes. Mutative methods produce a new collection as a new version of the old collection.

Transformations always return new collections

Mutative methods will return the same collection reference if nothing actually changes. This is what enables strict equality change detection. Transformation methods, on the other hand, don't have this capability. This means that if a transformation method results in the exact same collection values, it still returns a new reference. Let's look at the difference between mutated collections and transformed collections:

const myList = List.of(
  Map.of('one', 1, 'two', 2),
  Map.of('three', 3, 'four', 4),
  Map.of('five', 5, 'six', 6)
);
const myTransformedList = myList.map(v => v);
const myMutatedList = myList
  .update(0...