liveBook · Manning

Immutable collection

As explained in React Quickly, Second Edition

Data whose existing structures are not modified, with changes represented by creating new structures.

An immutable collection is data whose existing structure is not edited. Changes are represented by creating a new structure from the old one. This approach fits declarative programming, where expressions compose toward a result instead of manipulating shared objects step by step. section 1.1.1

Compare the two approaches:

  • Mutation edits an existing object or array, such as adding an item with push. section 1.1.1
  • Immutability derives a result from the original collection, such as filtering passwords without storing or editing the original array. section 1.1.1
  • React props follow the same read-only rule: a component can read its properties but should not edit or add them. section 2.5
function countGoodPasswords(passwords) {
 return passwords.filter(p => p.length >= 9).length;
}

This compact example derives a new filtered array and immediately reads its length, illustrating a non-mutating collection operation. section 1.1.1

Read props without changing them

A component may inspect this.props and use its values to render. React freezes the props object internally with Object.freeze(), so updates belong in the parent context rather than inside the component. section 2.5

How React Quickly, Second Edition covers it

How deep it goes
Defines mutation versus new-structure creation, demonstrates filtering, and stops short of advanced immutable-data techniques.
Examples
Uses password filtering, array push as a contrast, and frozen props inspected with Object.isFrozen().
What it assumes
Assumes basic JavaScript arrays and objects while teaching the distinction between mutation and declarative data transformation.
What you can do after
Apply it via array filter to derive results without editing the original collection or React props.
How it connects
Connects immutable data with declarative programming, array methods, React props, and functional components.

Also explained in

Compare how 24 books explain immutable collection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.