-
-
Notifications
You must be signed in to change notification settings - Fork 284
docs: add how to update values inside arrays #1193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
overthemike
merged 3 commits into
pmndrs:main
from
pooya-badiee:docs/how-to-update-values-inside-arrays
Jan 11, 2026
+93
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| --- | ||
| title: 'How to update values inside arrays' | ||
| --- | ||
|
|
||
| # How to update values inside arrays | ||
|
|
||
| Avoid unnecessary re-renders when updating values inside arrays. | ||
|
|
||
| ## Basic approach (triggers full list re-render) | ||
|
|
||
| In this example, iterating over the snapshot items causes the entire list to re-render whenever any item changes. | ||
|
|
||
| ```js | ||
| import { proxy, useSnapshot } from 'valtio' | ||
|
|
||
| const state = proxy({ | ||
| title: 'My Counter list', | ||
| items: [ | ||
| { id: 1, count: 0 }, | ||
| { id: 2, count: 0 }, | ||
| ], | ||
| }) | ||
|
|
||
| function Counter({ item }) { | ||
| return ( | ||
| <div> | ||
| <span>{item.count}</span> | ||
| <button onClick={() => item.count++}>+1</button> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function CounterList() { | ||
| const snap = useSnapshot(state) | ||
| return ( | ||
| <div> | ||
| <h1>{snap.title}</h1> | ||
| {/* Touching snap.items causes the whole list to re-render when any child updates */} | ||
| {snap.items.map((item) => ( | ||
| <Counter key={item.id} item={item} /> | ||
| ))} | ||
| </div> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ## Optimized approach (only changed items re-render) | ||
|
|
||
| Pass the proxy item (not snapshot) to children and let each child subscribe to its own item. | ||
| Do not access the entire array from the snapshot in the parent component. | ||
|
|
||
| ```js | ||
| import { proxy, useSnapshot } from 'valtio' | ||
|
|
||
| const state = proxy({ | ||
| title: 'My Counter list', | ||
| items: [ | ||
| { id: 1, count: 0 }, | ||
| { id: 2, count: 0 }, | ||
| ], | ||
| }) | ||
|
|
||
| function Counter({ item }) { | ||
| const snap = useSnapshot(item) | ||
| return ( | ||
| <div> | ||
| <span>{snap.count}</span> | ||
| <button onClick={() => item.count++}>+1</button> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function CounterList() { | ||
| const snap = useSnapshot(state) | ||
| return ( | ||
| <div> | ||
| <h1>{snap.title}</h1> | ||
| {/* Only the length is accessed from snap, so only updated children re-render */} | ||
| {Array.from({ length: snap.items.length }, (_, index) => ( | ||
| {/* Note that we are passing the proxy object (state) instead of the snapshot (snap) to the child component */} | ||
| <Counter key={state.items[index].id} item={state.items[index]} /> | ||
| ))} | ||
| </div> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ## Why this works | ||
|
|
||
| Because the proxy (in this case: `state.items[index]`) is a proxy object instead of a snapshot object, accessing it does not track usage. This prevents the parent component from "subscribing" to the item's internal changes, effectively isolating updates to the child component. | ||
|
|
||
| When you call `snap.items.map()`, you access every item in the array, causing Valtio to re-render whenever _any_ item changes. By only accessing `snap.items.length`, Valtio only re-renders when the array length changes. Using `Array.from({ length: n }, (_, i) => ...)` creates an array by index without touching snapshot items, letting you access the proxy directly. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.