Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita2073f3

Browse files
committed
docs: updateupdating-state docs with new changes
1 parent2e4925f commita2073f3

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

‎apps/docs/pages/store/updating-state.mdx‎

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -110,60 +110,66 @@ userStore.address.assign({
110110
});
111111
```
112112

113+
Note: the`assign` method only exists on object data type
114+
113115
##Using set with Callbacks
114116

115-
###store.set
117+
When passing a callback to to the`set` function, the type of callback you need to pass depends on the state datatype.
118+
119+
The types fully reflect which type of callback is required and therefore your IDE guide you.
120+
121+
###using`set(draft=>{})` with objects/arrays
116122

117-
Calling`.set` onthe store itself allowsyou to mutate a draft copy of the stateusing a callback function, and all changes will be applied immutably with immer.
123+
Calling`.set` onan object/array uses immer under the hook, allowingyou to mutate a draft copy of the statewhile maininaining immutability.
118124

119125
```tsx
126+
const userStore=store({ name:'John', age:20 });
127+
128+
// settings objects uses immer
120129
userStore.set((draft)=> {
121130
draft.name='Jane';
122131
draft.age=30;
123132
});
133+
134+
//
135+
const todosStore=store([
136+
{ id:1, text:'write docs' },
137+
{ id:2, text:'sleep' },
138+
]);
139+
140+
todosStore.set((draft)=> {
141+
draft.push('another task');
142+
});
143+
144+
todosStore.set(draft=>{
145+
cosntfirstTodo=todosStore[0];
146+
firstTodo.text="new text"
147+
})
124148
```
125149

126-
###store.property.set
150+
###Using `set(preValue=>...) with primatives
127151

128-
Calling`set` ona propertybehaves slightly differently than calling`set` on the store itself.
152+
Calling`set` onother data typesbehaves slightly differently.
129153
You are given the previous value of the property and you must return the new value from the callback.
130154

131155
```tsx
132156
userStore.age.set((prevAge)=>prevAge+1);
133157
```
134158

135-
###Differencesbetween`store.set` and`store.property.set`
159+
###Key differencesbetweenusingset callback
136160

137-
-`store.set` gets the entire state as a draft, you can directly modify the draft and you dont need to return anything
138-
-`store.property.set` gets the previous value of the property, you must return the new value from the callback
161+
- objects/arrays:
139162

140-
###Common pitfalls
163+
- you can directly modify the draft and you dont need to return anything
141164

142-
Since`set` uses immer under the hood, it is a good idea to familiarize yourself with[immerjs.github.io/immer/pitfalls](https://immerjs.github.io/immer/pitfalls) to avoid common pitfalls when using immer
165+
- primative values:
143166

144-
For example, when using array state make to use`store({items: []})` instead of`store([])` for arrays.
167+
- gets the previous value of the property, you must return the new value from the callback
145168

146-
```tsx
147-
const todosStore=store
148-
.state({
149-
todos: [
150-
{ id:1, text:'Buy milk', completed:false },
151-
{ id:2, text:'Do laundry', completed:false },
152-
],
153-
})
154-
.actions((store)=> ({
155-
addTodo(text) {
156-
store.todos.set((draft)=> {
157-
draft.push({ id:draft.length+1,text, completed:false });
158-
});
159-
},
160-
toggleTodo(id) {
161-
store.todos.set((draft)=> {
162-
const todo=draft.find((todo)=>todo.id===id);
163-
if (todo) {
164-
todo.completed=!todo.completed;
165-
}
166-
});
167-
},
168-
}));
169-
```
169+
##Additional notes:
170+
171+
- calling`.set(value)` on arrays/object behaves the same as other values. The difference only arises when passing a callback to the`set` function.
172+
173+
- Since`@davstack/store 1.3.0` root level array stores are fully supported
174+
175+
- You no longer need to consider the limitations of immer as non-draftable values eg MediaRecorder or window will default to regular zustand setters.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp