You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Setter returns a Promise; await to catch validation errors or ensure ordering
59
-
awaitsetState((prevState) => ({
60
-
todos: [
61
-
...prevState.todos,
62
-
{
63
-
text: 'New Todo',
64
-
status: "todo",
65
-
},
66
-
],
67
-
}));
68
-
};
76
+
const todos =useAtomValue(todosAtom);
77
+
const addTodo =useSetAtom(addTodoAtom);
69
78
70
79
return (
71
80
<div>
72
-
<buttononClick={addTodo}>Add Todo</button>
81
+
<buttononClick={()=>{
82
+
addTodo({text: "New todo", status: "todo"})
83
+
}}>Add Todo</button>
73
84
<ul>
74
85
{state.todos.map((todo) => (
75
86
<likey={todo.$cid/* stable key from Loro container id */}>
@@ -84,13 +95,17 @@ function TodoApp() {
84
95
85
96
### Async behavior
86
97
87
-
- The setter returned by `useAtom(loroMirrorAtom(...))` returns a Promise. Await it when you need deterministic ordering or to handle validation/consistency errors.
98
+
- The setter returned by `useAtom(loroMirrorAtom(...))` returns a Promise. Await
99
+
it when you need deterministic ordering or to handle validation/consistency
100
+
errors.
88
101
- You can also pass `onError` in the atom config to catch rejections centrally.
89
102
90
103
### About `$cid`
91
104
92
-
-`$cid` is always present on `LoroMap` state and equals the underlying Loro container id.
93
-
- Use `$cid` as a stable list selector and React key: `schema.LoroList(item, x => x.$cid)` and `<li key={todo.$cid}>`.
105
+
-`$cid` is always present on `LoroMap` state and equals the underlying Loro
106
+
container id.
107
+
- Use `$cid` as a stable list selector and React key:
108
+
`schema.LoroList(item, x => x.$cid)` and `<li key={todo.$cid}>`.
0 commit comments