Immutable Data Types


Learning Objectives

After this lesson, you will be able to:

  • Define the meaning of immutable data.
  • List common mutable and immutable data types.

What Is Immutable?

Immutable Meme
Immutable Meme

What Is Immutable Data?


Arrays Can Mutate!


Other Array Methods That Can Mutate The Array That Calls Them

  • push()
  • reverse()
  • unshift()
  • splice()

Immutable Data in React

Don’t Do:


Immutable Data in React

Do:


Immutable Data and Props

Component props should not (and cannot) be changed within a component.


Immutable Methods

To pop() an array without mutating it, you could do this:


Immutable Methods


Array and Object Spread

The spread operator: ...


Cloning Arrays Using the Spread Operator


Pushing Data to an Array Using the Spread Operator


Cloning and Adding Data in an Object Using the Spread Operator


Immutable Libraries

Immutable.js provides List, Stack, Map, OrderedMap, Set, OrderedSet and Record — along with methods for making changes to your data like set(), get(), delete(), update(), etc.


Additional Resources: