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

Mapping behavior


In imperative code, you have things, such as if statements, that determine what happens next. Taken in moderation, there's nothing wrong with this type of construct. But when your main avenue of logic is imperative statements, things tend to grow unmaintainable quickly.

The alternative to this imperative style is to compose your logic declaratively. Maps are the first tool that will help you get there.

Keys are logical paths, values are behavior

When you use maps as the means to declare your application logic, it's helpful to think of the map keys as the possible paths. For example, an if statement checks whether a condition is true, and if so, it allows for a block of code to be executed. With maps, keys are the condition. The values with which they're paired represent the functionality to execute when the condition is true.

Let's illustrate this idea at it's most basic level using an Immutable.js map:

const actions = Map.of(
  'first', () => console.log('first'),
  'second...