SlideShare a Scribd company logo
Immutability & Javascript
Nadia Miętkiewicz / Visuality
Why this talk?
● immutable.js is included by default in react-boilerplate
● that redux-form bug...
that redux-form bug
import React from 'react';
import { Field, reduxForm } from 'redux-form';
const Form = (props) => (
<form onSubmit={props.handleSubmit}>
<Field name="email" component="input" />
<button type="submit">Sign in</button>
</form>
);
export default reduxForm({
form: 'signIn',
})(Form);
import React from 'react';
import { Field, reduxForm } from 'redux-form/immutable';
const Form = (props) => (
<form onSubmit={props.handleSubmit}>
<Field name="email" component="input" />
<button type="submit">Sign in</button>
</form>
);
export default reduxForm({
form: 'signIn',
})(Form);
The case for immutability
Much of what makes application development difficult is
tracking mutation and maintaining state.
Muttable
const item = {
shape: 'square',
position: 'A',
};
Muttable
const item = {
shape: 'square',
position: 'A',
};
item.position = 'B';
Immuttable
const item = {
shape: 'square',
position: 'A',
};
const item2 = {
...item,
position: 'B',
};
Prestige (2006)
hughJackman.position = 'balcony';
Prestige (2006)
const hughJackmanClone = {
...hughJackman,
position: 'balcony',
};
Why not directly?
const item = {
shape: 'human',
position: 'A',
};
item.position = 'B';
Mutator methods
const item = {
shape: 'human',
position: 'A',
};
item.position = 'B';
Mutator methods
const item = {
shape: 'human',
position: 'A',
};
teleport(item, 'B');
function teleport(subject, position) {
subject.position = position;
}
Mutator methods
const item = {
shape: 'human',
position: 'A',
};
teleport(item, 'B');
function teleport(subject, position) {
/***********************************************/
YOLO
¯_(ツ)_/¯
/***********************************************/
}
The Fly (1986)
function teleport(subject, position) {
subject.position = position;
items = getCapsuleContents(); // should be [ ‘human’ ]
subject.shape = items.join();
}
The Fly (1986)
function teleport(subject, position) {
subject.position = position;
items = getCapsuleContents(); // is [ ‘human’, ‘fly’ ]
subject.shape = items.join();
}
What about memory
consumption?
Memory consumption
const item = {
shape: 'square',
position: 'A',
};
const item2 = {
...item,
position: 'B',
};
Memory consumption
const item = {
shape: 'square',
position: 'A',
};
const item2 = {
...item,
position: 'B',
};
const item3 = {
...item2,
position: 'C',
};
const item4 = {
...item,
position: 'D',
};
const item5 = {
...item,
position: 'E',
};
const item6 = {
...item2,
position: 'F',
};
Prestige (2016)
const hughJackmanClone = {
...hughJackmanOriginal,
position: 'balcony',
};
drown(hughJackmanOriginal);
Star Trek (1966 - 2005)
Persistent Data Structures
Structural Sharing
Structural Sharing
Structural Sharing
Structural Sharing
Immutable JS
Map
import { Map } from 'immutable';
const item = Map({
shape: 'square',
position: 'A',
});
const item2 = item.set('position', 'B');
const item = {
shape: 'square',
position: 'A',
};
item.position = 'B';
List
import { List } from 'immutable';
const items = List([
'apple',
'banana',
]);
const items2 = item.push('orange');
const items = [
'apple',
'banana',
];
items.push('orange');
make immutable
import { fromJS } from 'immutable';
const nested = fromJS({ a: { b: { c: [ 3, 4, 5 ] } } });
// Map { a: Map { b: Map { c: List [ 3, 4, 5 ] } } }
make mutable
import { Map, List } from 'immutable';
const deep = Map({ a: 1, b: 2, c: List([ 3, 4, 5 ]) });
console.log(deep.toObject()); // { a: 1, b: 2, c: List [ 3, 4, 5 ] }
console.log(deep.toArray()); // [ 1, 2, List [ 3, 4, 5 ] ]
console.log(deep.toJS()); // { a: 1, b: 2, c: [ 3, 4, 5 ] }
toJS() controversy
Debugging
Where to use?
● For the entire Redux state tree
● Your selectors should return Immutable.JS objects
● Never use toJS() in mapStateToProps
● Always in Smart Components (containers)
● Never in Dumb Components
● Wrap Dumb Components in a HOC to convert the props
Thank You!

More Related Content

PDF
Polymorphism
DOCX
informatics practices practical file
DOC
project report in C++ programming and SQL
PDF
SATySFiのこれからの課題たち
PPTX
C Programming Language Part 4
PDF
Informatics Practices/ Information Practices Project (IP Project Class 12)
DOCX
Computer Science Practical Science C++ with SQL commands
PPTX
C Programming Language Step by Step Part 5
Polymorphism
informatics practices practical file
project report in C++ programming and SQL
SATySFiのこれからの課題たち
C Programming Language Part 4
Informatics Practices/ Information Practices Project (IP Project Class 12)
Computer Science Practical Science C++ with SQL commands
C Programming Language Step by Step Part 5

What's hot (18)

DOC
Practical Class 12th (c++programs+sql queries and output)
PPTX
Types of Constructor in C++
PDF
Super TypeScript II Turbo - FP Remix (NG Conf 2017)
DOCX
Check the output of the following code then recode it to eliminate fu
PPTX
C++ project
PDF
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
PDF
Lec 7 28_aug [compatibility mode]
DOCX
C++ file
PDF
Writing Macros
PDF
C++ TUTORIAL 2
PDF
Introduction to Computer and Programing - Lecture 04
PPTX
Java(Access Modifiers)
PDF
C programs
PDF
The Ring programming language version 1.5.3 book - Part 188 of 194
PPTX
Functional DDD
DOC
programming in C++ report
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
Practical Class 12th (c++programs+sql queries and output)
Types of Constructor in C++
Super TypeScript II Turbo - FP Remix (NG Conf 2017)
Check the output of the following code then recode it to eliminate fu
C++ project
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Lec 7 28_aug [compatibility mode]
C++ file
Writing Macros
C++ TUTORIAL 2
Introduction to Computer and Programing - Lecture 04
Java(Access Modifiers)
C programs
The Ring programming language version 1.5.3 book - Part 188 of 194
Functional DDD
programming in C++ report
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
Ad

Similar to Immutability and Javascript - Nadia Miętkiewicz (20)

PDF
Angular2: Quick overview with 2do app example
PDF
Higher-Order Components — Ilya Gelman
PDF
What is new in sulu 2.0
PDF
Taming forms with React
PDF
PROGRAMING IN JAVA 4TH SEM DIGVIJAY COLLAGE
PDF
Introductionandgreetings
PDF
Modern Web Developement
PPTX
Fact, Fiction, and FP
PDF
Creating an Uber Clone - Part XXIV.pdf
PDF
[FEConf Korea 2017]Angular 컴포넌트 대화법
PDF
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
PDF
This is Java,I am currently stumped on how to add a scoreboard for.pdf
PDF
I dont know what is wrong with this roulette program I cant seem.pdf
PDF
The Human Linter — Ilya Gelman
PDF
The Human Linter
PDF
Introduction to ECMAScript 2015
PDF
An introduction to Angular2
PDF
Higher Order Components and Render Props
PDF
Bindings: the zen of montage
Angular2: Quick overview with 2do app example
Higher-Order Components — Ilya Gelman
What is new in sulu 2.0
Taming forms with React
PROGRAMING IN JAVA 4TH SEM DIGVIJAY COLLAGE
Introductionandgreetings
Modern Web Developement
Fact, Fiction, and FP
Creating an Uber Clone - Part XXIV.pdf
[FEConf Korea 2017]Angular 컴포넌트 대화법
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
The Human Linter — Ilya Gelman
The Human Linter
Introduction to ECMAScript 2015
An introduction to Angular2
Higher Order Components and Render Props
Bindings: the zen of montage
Ad

More from Visuality (20)

PPTX
3 issues that made 30 test workers take 40 minutes
PPTX
Czego nie robić przy pisaniu testów
PDF
Introduction to Domain-Driven Design in Ruby on Rails
PDF
Active Record .includes - do you use it consciously?
PDF
Introduction to Event Storming
PDF
Jak programowanie może pomóc na co dzień?
PDF
SVG Overview - How To Draw, Use and Animate
PDF
How To Migrate a Rails App From a Dedicated Server Into Cloud Environment? - ...
PPTX
How to use AWS SES with Lambda 
in Ruby on Rails application - Michał Łęcicki
PPTX
What is NOT machine learning - Burak Aybar
PPTX
Do you really need to reload?
PDF
How to check valid email? Find using regex(p?)
PPTX
Fantastic stresses and where to find them
PDF
Fuzzy search in Ruby
PDF
Rfc process in visuality
PDF
GraphQL in Ruby on Rails - basics
PPTX
Consumer Driven Contracts
PDF
How do we use CircleCi in Laterallink?
PDF
React Native - Short introduction
PDF
Risk in project management
3 issues that made 30 test workers take 40 minutes
Czego nie robić przy pisaniu testów
Introduction to Domain-Driven Design in Ruby on Rails
Active Record .includes - do you use it consciously?
Introduction to Event Storming
Jak programowanie może pomóc na co dzień?
SVG Overview - How To Draw, Use and Animate
How To Migrate a Rails App From a Dedicated Server Into Cloud Environment? - ...
How to use AWS SES with Lambda 
in Ruby on Rails application - Michał Łęcicki
What is NOT machine learning - Burak Aybar
Do you really need to reload?
How to check valid email? Find using regex(p?)
Fantastic stresses and where to find them
Fuzzy search in Ruby
Rfc process in visuality
GraphQL in Ruby on Rails - basics
Consumer Driven Contracts
How do we use CircleCi in Laterallink?
React Native - Short introduction
Risk in project management

Recently uploaded (20)

DOCX
ENGLISH PROJECT FOR BINOD BIHARI MAHTO KOYLANCHAL UNIVERSITY
PPTX
Tour Presentation Educational Activity.pptx
PDF
oil_refinery_presentation_v1 sllfmfls.pdf
PPT
First Aid Training Presentation Slides.ppt
PDF
Swiggy’s Playbook: UX, Logistics & Monetization
PPTX
Hydrogel Based delivery Cancer Treatment
PDF
Presentation1 [Autosaved].pdf diagnosiss
PPTX
worship songs, in any order, compilation
PPTX
2025-08-10 Joseph 02 (shared slides).pptx
PPTX
Introduction to Effective Communication.pptx
PPTX
Project and change Managment: short video sequences for IBA
PPTX
Non-Verbal-Communication .mh.pdf_110245_compressed.pptx
PPTX
Effective_Handling_Information_Presentation.pptx
PPTX
Intro to ISO 9001 2015.pptx wareness raising
PPTX
fundraisepro pitch deck elegant and modern
PPTX
ART-APP-REPORT-FINctrwxsg f fuy L-na.pptx
PPTX
Learning-Plan-5-Policies-and-Practices.pptx
PPTX
INTERNATIONAL LABOUR ORAGNISATION PPT ON SOCIAL SCIENCE
PPTX
Human Mind & its character Characteristics
PPTX
Role and Responsibilities of Bangladesh Coast Guard Base, Mongla Challenges
ENGLISH PROJECT FOR BINOD BIHARI MAHTO KOYLANCHAL UNIVERSITY
Tour Presentation Educational Activity.pptx
oil_refinery_presentation_v1 sllfmfls.pdf
First Aid Training Presentation Slides.ppt
Swiggy’s Playbook: UX, Logistics & Monetization
Hydrogel Based delivery Cancer Treatment
Presentation1 [Autosaved].pdf diagnosiss
worship songs, in any order, compilation
2025-08-10 Joseph 02 (shared slides).pptx
Introduction to Effective Communication.pptx
Project and change Managment: short video sequences for IBA
Non-Verbal-Communication .mh.pdf_110245_compressed.pptx
Effective_Handling_Information_Presentation.pptx
Intro to ISO 9001 2015.pptx wareness raising
fundraisepro pitch deck elegant and modern
ART-APP-REPORT-FINctrwxsg f fuy L-na.pptx
Learning-Plan-5-Policies-and-Practices.pptx
INTERNATIONAL LABOUR ORAGNISATION PPT ON SOCIAL SCIENCE
Human Mind & its character Characteristics
Role and Responsibilities of Bangladesh Coast Guard Base, Mongla Challenges

Immutability and Javascript - Nadia Miętkiewicz

  • 1. Immutability & Javascript Nadia Miętkiewicz / Visuality
  • 2. Why this talk? ● immutable.js is included by default in react-boilerplate ● that redux-form bug...
  • 3. that redux-form bug import React from 'react'; import { Field, reduxForm } from 'redux-form'; const Form = (props) => ( <form onSubmit={props.handleSubmit}> <Field name="email" component="input" /> <button type="submit">Sign in</button> </form> ); export default reduxForm({ form: 'signIn', })(Form); import React from 'react'; import { Field, reduxForm } from 'redux-form/immutable'; const Form = (props) => ( <form onSubmit={props.handleSubmit}> <Field name="email" component="input" /> <button type="submit">Sign in</button> </form> ); export default reduxForm({ form: 'signIn', })(Form);
  • 4. The case for immutability Much of what makes application development difficult is tracking mutation and maintaining state.
  • 5. Muttable const item = { shape: 'square', position: 'A', };
  • 6. Muttable const item = { shape: 'square', position: 'A', }; item.position = 'B';
  • 7. Immuttable const item = { shape: 'square', position: 'A', }; const item2 = { ...item, position: 'B', };
  • 9. Prestige (2006) const hughJackmanClone = { ...hughJackman, position: 'balcony', };
  • 11. const item = { shape: 'human', position: 'A', }; item.position = 'B'; Mutator methods
  • 12. const item = { shape: 'human', position: 'A', }; item.position = 'B'; Mutator methods const item = { shape: 'human', position: 'A', }; teleport(item, 'B'); function teleport(subject, position) { subject.position = position; }
  • 13. Mutator methods const item = { shape: 'human', position: 'A', }; teleport(item, 'B'); function teleport(subject, position) { /***********************************************/ YOLO ¯_(ツ)_/¯ /***********************************************/ }
  • 14. The Fly (1986) function teleport(subject, position) { subject.position = position; items = getCapsuleContents(); // should be [ ‘human’ ] subject.shape = items.join(); }
  • 15. The Fly (1986) function teleport(subject, position) { subject.position = position; items = getCapsuleContents(); // is [ ‘human’, ‘fly’ ] subject.shape = items.join(); }
  • 17. Memory consumption const item = { shape: 'square', position: 'A', }; const item2 = { ...item, position: 'B', };
  • 18. Memory consumption const item = { shape: 'square', position: 'A', }; const item2 = { ...item, position: 'B', }; const item3 = { ...item2, position: 'C', }; const item4 = { ...item, position: 'D', }; const item5 = { ...item, position: 'E', }; const item6 = { ...item2, position: 'F', };
  • 19. Prestige (2016) const hughJackmanClone = { ...hughJackmanOriginal, position: 'balcony', }; drown(hughJackmanOriginal);
  • 20. Star Trek (1966 - 2005)
  • 27. Map import { Map } from 'immutable'; const item = Map({ shape: 'square', position: 'A', }); const item2 = item.set('position', 'B'); const item = { shape: 'square', position: 'A', }; item.position = 'B';
  • 28. List import { List } from 'immutable'; const items = List([ 'apple', 'banana', ]); const items2 = item.push('orange'); const items = [ 'apple', 'banana', ]; items.push('orange');
  • 29. make immutable import { fromJS } from 'immutable'; const nested = fromJS({ a: { b: { c: [ 3, 4, 5 ] } } }); // Map { a: Map { b: Map { c: List [ 3, 4, 5 ] } } }
  • 30. make mutable import { Map, List } from 'immutable'; const deep = Map({ a: 1, b: 2, c: List([ 3, 4, 5 ]) }); console.log(deep.toObject()); // { a: 1, b: 2, c: List [ 3, 4, 5 ] } console.log(deep.toArray()); // [ 1, 2, List [ 3, 4, 5 ] ] console.log(deep.toJS()); // { a: 1, b: 2, c: [ 3, 4, 5 ] }
  • 33. Where to use? ● For the entire Redux state tree ● Your selectors should return Immutable.JS objects ● Never use toJS() in mapStateToProps ● Always in Smart Components (containers) ● Never in Dumb Components ● Wrap Dumb Components in a HOC to convert the props