-
-
Notifications
You must be signed in to change notification settings - Fork 622
/
migrating-to-v2-api.mdx
233 lines (163 loc) · 4.7 KB
/
migrating-to-v2-api.mdx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
---
title: v2 API migration
description: New "Async" API
nav: 7.0
---
RFC: https://github.com/pmndrs/jotai/discussions/1514
Jotai v1 is released at June 2022, and there has been various feedbacks.
React also proposes first-class support for promises.
Jotai v2 will have a new API.
Unfortunately, there are some breaking changes along with new features.
## What are new features
### Vanilla library
Jotai comes with vanilla (non-React) functions
and React functions separately.
They are provided from alternate entry points like `jotai/vanilla`.
### Store API
Jotai exposes store interface so that you can directly manipulate atom values.
```js
import { createStore } from 'jotai' // or from 'jotai/vanilla'
const store = createStore()
store.set(fooAtom, 'foo')
console.log(store.get(fooAtom)) // prints "foo"
const unsub = store.sub(fooAtom, () => {
console.log('fooAtom value in store is changed')
})
// call unsub() to unsubscribe.
```
You can also create your own React Context to pass a store.
### More flexible atom `write` function
The write function can accept multiple arguments,
and return a value.
```js
atom(
(get) => get(...),
(get, set, arg1, arg2, ...) => {
...
return someValue
}
)
```
## What are breaking
### Async atoms are no longer special
Async atoms are just normal atoms with promise values.
Atoms getter functions don't resolve promises.
On the other hand, `useAtom` hook continues to resolve promises.
### Writable atom type is changed (TypeScript only)
```ts
// Old
WritableAtom<Value, Arg, Result extends void | Promise<void>>
// New
WritableAtom<Value, Args extends unknown[], Result>
```
In general, we should avoid using `WritableAtom` type directly.
### Some functions are dropped
- Provider's `initialValues` prop is removed, because `store` is more flexible.
- Provider's scope props is removed, because you can create own context.
- `abortableAtom` util is removed, because the feature is included by default
- `waitForAll` util is removed, because `Promise.all` just works
## Migration guides
### Async atoms
`get` function for read function of async atoms
doesn't resolve promises, so you have to put `await` or `.then()`.
In short, the change is something like the following.
(If you are TypeScript users, types will tell where to changes.)
#### Previous API
```js
const asyncAtom = atom(async () => 'hello')
const derivedAtom = atom((get) => get(asyncAtom).toUppercase())
```
#### New API
```js
const asyncAtom = atom(async () => 'hello')
const derivedAtom = atom(async (get) => (await get(asyncAtom)).toUppercase())
// or
const derivedAtom = atom((get) => get(asyncAtom).then((x) => x.toUppercase()))
```
### Provider's `initialValues` prop
#### Previous API
```jsx
const countAtom = atom(0)
// in component
<Provider initialValues={[[countAtom, 1]]}>
...
```
#### New API
```jsx
const countAtom = atom(0)
const HydrateAtoms = ({ initialValues, children }) => {
useHydrateAtoms(initialValues)
return children
}
// in component
<Provider>
<HydrateAtoms initialValues={[[countAtom, 1]]}>
...
```
### Provider's `scope` prop
#### Previous API
```jsx
const myScope = Symbol()
// Parent component
<Provider scope={myScope}>
...
</Provider>
// Child component
useAtom(..., myScope)
```
#### New API
```jsx
const MyContext = createContext()
const store = createStore()
// Parent component
<MyContext.Provider value={store}>
...
</MyContext.Provider>
// Child Component
const store = useContext(MyContext)
useAtom(..., { store })
```
### `abortableAtom` util
You no longer need the previous `abortableAtom` util,
because it's now supported with the normal `atom`.
#### Previous API
```js
const asyncAtom = abortableAtom(async (get, { signal }) => {
...
}
```
#### New API
```js
const asyncAtom = atom(async (get, { signal }) => {
...
}
```
### `waitForAll` util
You no longer need the previous `waitForAll` util,
because we can use native Promise APIs.
#### Previous API
```js
const allAtom = waitForAll([fooAtom, barAtom])
```
#### New API
```js
const allAtom = atom((get) => Promise.all([get(fooAtom), get(barAtom)]))
```
## Some other changes
### Utils
- `atomWithStorage` util's `delayInit` is removed as being default.
- `useHydrateAtoms` can only accept writable atoms.
### Import statements
The v2 API is also provided from alternate entry points for library authors and non-React users.
- `jotai/vanilla`
- `jotai/vanilla/utils`
- `jotai/react`
- `jotai/react/utils`
```js
// Available since v1.11.0
import { atom } from 'jotai/vanilla'
import { useAtom } from 'jotai/react'
// Available since v2.0.0
import { atom } from 'jotai' // is same as 'jotai/vanilla'
import { useAtom } from 'jotai' // is same as 'jotai/react'
```