Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Immutable update
- Producing a new value with a change instead of changing the existing value.
- Structural sharing
- A new value reusing the unchanged parts of the old one instead of copying them.
- Persistent data
- Data whose earlier versions stay available and unchanged after an update.
- Path copy
- Building new values for every level between the outer value and the changed part.
- Snapshot
- A saved version that nothing will change afterwards.
An update that retains the old version
An immutable update produces a new version while keeping the earlier one usable. Persistent data retains earlier versions across updates; structural sharing can reuse unchanged parts instead of copying everything.
That only works if an update builds a new value instead of changing the old one, including any part the two still share. A fresh outer container does not establish independence of its nested data. Follow the actual field types and copying operations before writing through a retained reference.
01Updating without mutation
An update returns a new value and leaves the old one alone
A trip planner keeps the planned trip and the updated one side by side, and keeps every earlier plan so an edit can be undone.
The planner first proposes a longer trip while retaining the planned duration. Predict both day counts, then inspect what the selected language’s comparison expression actually compares.
type Trip = { city: string; days: number };
const withDays = (trip: Trip, days: number): Trip => ({ ...trip, days });
const trip: Trip = { city: "Oslo", days: 3 };
const later = withDays(trip, 5);
console.log(trip.days, later.days, trip === later);
npx tsc --noEmit --strict --target ES2022 --module nodenext --moduleDetection force main.ts && node main.ts
Use the selected language’s command. Trace both the returned value and every access path the caller retains; a writable local binding does not by itself say which storage is shared.
03Keeping versions
A history keeps versions only if later edits cannot reach them
Undo restores an earlier state only if its saved storage survives later writes. This flat-plan probe saves before and after a duration change. Predict what the saved entries retain.
type Trip = { city: string; days: number };
const history: Trip[] = [];
const trip: Trip = { city: "Oslo", days: 3 };
history.push(trip);
trip.days = 5;
history.push(trip);
console.log(history[0]!.days, history[1]!.days);
04Each language’s trap
Read-only, receivers, moves and fluent setters
Each program looks like it keeps the old value intact. Predict what happens.
type Trip = { days: number; stops: string[] };
const trip: Trip = Object.freeze({ days: 3, stops: ["Bergen"] });
trip.stops.push("Oslo");
console.log(trip.stops.length, Object.isFrozen(trip));
05Repair the trip planner
More days, another night, then undo
The planner needs independent proposals for two more days and an extra hotel night. Its history then needs to restore complete earlier plans after duration edits, including edits made after undoing a previous change.
Each file contains its imports, reader and complete caller.
Repair the trip planner
Compare planned and proposed days, retain the nested hotel plan, then restore complete earlier versions.
In the frontend Send a proposed value back to its owner Optional
A client parent passes a flat planned trip and a proposal callback. This button sends a new city/days value; the parent decides whether to store or display it. The child does not assign to the planned prop.
"use client";
type Trip = { city: string; days: number };
type Props = { planned: Trip; onPropose: (next: Trip) => void };
export default function MoreDays({ planned, onPropose }: Props) {
return (
<button onClick={() => onPropose({ ...planned, days: planned.days + 1 })}>
Propose one more day
</button>
);
}
The click handler creates a new ordinary object from the current prop’s primitive fields. Calling onPropose hands that value to the client parent.
Sources: React: Passing props,Svelte: Props.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?