About This Article
This article is created through an automated generation workflow utilizing generative AI. Implementation examples from existing materials were cross-referenced with official documentation to organize operation examples, constraints, and reference test results.Validation Status: Partially cross-referenced with official documentation and verified via reference tests. Browser execution of the static demo version has not been confirmed.
When changing a single form setting, it is convenient to be able to revert to the previous value. Using Immer, we modify only a single nested location and verify the unedited parts.
- Terminology to Know First
- Thinking in Terms of Class Diagrams
- Live Demo Operation
- Key Points of the Code
- When using in production
- Viewing the original A and the new A simultaneously
- What to copy when updating nested values
- Is it permissible to modify the shared B?
- Applying to form change history
- Verification Scope
- References
- GitHub Sample
Terminology to Know First
Immutableis an approach that handles data without directly rewriting existing values.
Referenceis information pointing to the same object.
Thinking in Terms of Class Diagrams
The boxes represent data structures before and after the update. The arrows pointing to B converge on the same object.
classDiagram
class Before {
A value 1
B value 2
}
class After {
A value 9
B value 2
}
class SharedB
Before --> SharedB : B
After --> SharedB : B
Live Demo Operation
Set the value of A to 9. Confirm that the original value matches B.
How to Read the Results
Original A remains 1. B shares the same object.
Sharing unupdated B reduces the scope of copying. Avoid designs where B is mutated from separate processes.
Key Points of the Code
Update A of the draft within produce, and display the original and updated values.
The implementation can be checked in the implementation code of the demo screen.
When using in production
When tracking state diffs across the screen, preserve the pre-update and post-update states separately.
Viewing the original A and the new A simultaneously
Press Execute with the initial value of 9. The results section displays the pre-update object, post-update object, and whether the reference to B is the same. We expect A's value to be 1 and B's value to be 2 before the update, and A's value to be 9 and B's value to be 2 after the update. The condition where B's reference remains the same is expected to be true. Distinguish between the numbers displayed on the screen being equal and referencing the exact same object.
Next, enter 1 and execute. Because you assign the same value as the original A, the visual result does not change. Since the demo only displays the identity of B, you cannot determine whether the references for A or the root object are identical based solely on the display output. If necessary, verify locally using strict equality operators for the root and A as well. In the subsequent execution, the initial object is re-created, so specifying 1 after setting it to 9 does not mean the history is preserved between executions.
What to copy when updating nested values
JavaScript objects are handled through references. Even if you only shallow copy the root, inner properties like A and B might still reference the original objects. If you directly modify A on the new side in that state, you risk altering the original A as well. To preserve the original state, you must also appropriately copy the objects along the modification path. The deeper the nesting, the easier it is to overlook places that require manual copying.
Immer's produce accepts the original state and a function describing the updates. Writing modifications to the draft passed to the function lets you receive the updated result as the next state. It is not a function designed to mutate the original state directly. Unmodified parts are shared, so you do not need to recreate everything using deep copies. This code modifies only d.A.value and checks the unchanged reference of B via a.B === b.B. Combining value comparison with reference comparison is meaningful.
Is it permissible to modify the shared B?
If B's value remains 2 and its reference is identical, both the old and new states are looking at the same data. Unsystematically mutating shared parts from separate logic contradicts an architecture that maintains old states. Depending on Immer configuration, object freezing may apply, but rather than relying solely on that, consolidate state mutation pathways into functions like produce. Sharing is a mechanism that aids memory and comparison efficiency, not an invitation for anyone to mutate it directly.
Entering abc into the input field causes the implementation's Number conversion to yield NaN. When the result is displayed via JSON.stringify, NaN becomes null, so be careful not to mistake this for actually storing null in the real state. Empty fields or whitespace may also be converted to 0. In practical numeric inputs, distinguish empty fields, verify whether they are finite numbers, and check business acceptability before updating. Input validation is separate from Immer's role.
Applying to form change history
For example, if you edit part of an address in an application form and want to revert to the previous state using an undo operation, it is convenient to manage the pre-change and post-change versions as separate states. However, simply calling produce does not automatically provide a history list or an undo button. The application side must design which point in time to save, how to limit the number of saved items, and how to handle personal information.
If no display appears, first revert to the initial value of 9, and check the Console and Network tabs to see if external modules were successfully fetched. Inspect normal and abnormal values separately, and always confirm that the original state remains unchanged. Public demos should only use dummy numbers. This local inspection is a reference test confirming equivalent reference sharing via the spread syntax, and does not measure Immer's actual internal operations.
Verification Scope
Reference implementation using spread in Node.js 22.16.0: asserted pre-update A=1, post-update A=9, and B reference identity. Confirmed that the JSON representation of Number('abc') becomes null. The core Immer produce execution was omitted.
Targets current browsers and the course-specified Immer 10.1.1. Network communication for external CDN and Mermaid loading is required, and re-confirming the course's actual display, actual diagram rendering, and fixed-version licenses remains pending. Because official documentation also contains descriptions of other versions, this must be distinguished from proof of fixed-version execution.
References
Official API and Documentation(Accessed November 30, 2026)
