- Daishi Kato's Read the Code
- Posts
- Jotai v3 is Mostly a Migration Release
Jotai v3 is Mostly a Migration Release
With one nuanced behavioral change
Hi,
Jotai v3.0.0 came out yesterday. My original ETA was earlier this year, so it took longer than expected. This is mostly a maintenance release, dropping some legacy support.
But that’s not the only change. I made one meaningful behavioral change.
Waiting for use(store)
Actually, I was hoping to land the use(store) proposal:
That research still seems incomplete, so I decided to make v3 an intermediate step.
Jotai tries to be concurrent React friendly, and it doesn’t use useSyncExternalStore:
The extra re-render in Jotai v2
To make this work, Jotai v2 has one extra line that triggers a re-render, which usually bails out, in the useAtomValue hook. There’s a performance drawback to it, and it has been reported several times.
My idea was to remove that line with an escape hatch. And the escape hatch is useSyncExternalStore, which is a very proper solution in this case.
Jotai v2 supports React 17, so I had to wait for Jotai v3, which drops React 17 support.
Three hooks in Jotai v3
Now with Jotai v3, we have three different hooks. This also covers the use(atomValue) use case.
useAtomValueuseAtomValueRawuseAtomValueRawSync
To be honest, I’m not a big fan of this API design. Having users select which hook to use is really bad. But it can’t be helped at this point.
It feels like the best middle ground while keeping compatibility with Jotai v2.
We’ll see how it goes in the future!
Happy coding.
Reply