yarn jotai 1.4.0
v1.4.0

latest releases: 2.8.0, 2.7.2, 2.7.1...
2 years ago

Atom types are improved (BREAKING CHANGE in types)

Previously, sync atom and async atom are not distinguishable by types. This is improved now. If you make types inferred, there would be no changes required. If you explicitly type async atoms, migration would be required.

Migration Guide

Async writable atom

Previously, when you annotate atom() to create a writable atom, it looks like this:

const atom1 = atom(0)
const atom2 = atom<number, number>(
  (get) => get(atom1), 
  async (get, set, arg) => set(atom1, arg),
)

☝️ That will be type error.

A fix would be adding the 3rd type arg:

const atom2 = atom<number, number, Promise<void>>(
  (get) => get(atom1), 
  async (get, set, arg) => set(atom1, arg),
)

But, the recommendation is not to annotate atom() types, but arg only:

const atom2 = atom(
  (get) => get(atom1), 
  async (get, set, arg: number) => set(atom1, arg),
)

Async atom (read function)

Previously, async (read) atoms are typed like this:

const atom3 = atom<number>(async (get) => get(atom1))

☝️ That will not work.

A fix would be annotate it with Promsie<Value>:

const atom3 = atom<Promise<number>>(async (get) => get(atom1))

But, the recommendation is not to annotate atom() types, but to infer types:

const atom3 = atom(async (get) => get(atom1))

Async write atoms no longer suspend (BREAKING CHANGE in behavior)

Suspending on write turns out to be a bit of trouble. We should use promises.
If you depend on this behavior, you might need to do something.

Migration Guide

Previously, an async write atom suspends (triggers Suspense fallback):

const atom1 = atom(null, async (get, set, arg) => {
  // async task
})

☝️ That will not suspend any longer.

We should instead have a loading flag.

const pendingAtom = atom(false)
const atom1 = atom(null, async (get, set, arg) => {
  set(pendingAtom, true)
  // async task
  set(pendingAtom, false) // or put in finally clause
})

What's Changed

  • breaking(types): refine atom type and other types by @dai-shi in #713
  • fix(core): no async write suspense (BREAKING CHANGE in behavior) by @dai-shi in #731
  • breaking(utils): remove deprecated signature of atomWithHash by @dai-shi in #763
  • fix(utils): resolve undefined observable by @dai-shi in #777

New Contributors

Full Changelog: v1.3.9...v1.4.0

Don't miss a new jotai release

NewReleases is sending notifications on new releases.