Jotai advantages over Recoil #849
Replies: 11 comments 12 replies
-
This is a tricky question. It might be good to continue discussion instead of just answering once. I hope other people to jump in and have questions and answers. For now, I have one technical aspect for an answer. Recoil atoms are distinguished by string keys and jotai atoms are by object references. Jotai's implementation is based on WeakMap, and data is garbage collected by JS engine. This simplifies the implementation quite a bit. This will be a difference that lasts for a long time, maybe forever. |
Beta Was this translation helpful? Give feedback.
-
A few advantages of Jotai over Recoil off the top of my head:
There is also an article on the topic with more information. |
Beta Was this translation helpful? Give feedback.
-
I believe one of the major differences, besides all the previous mentioned, is that Jotai is TypeScript focused and there's a focus on delivering good DX in a TypeScript environment. |
Beta Was this translation helpful? Give feedback.
-
I haven't tried Jotai yet - but just been pretty heavily burned by Recoil. Recoil essentially holds on to every piece of state it is ever given. See these GitHub issues about memory leaks: facebookexperimental/Recoil#1064 facebookexperimental/Recoil#1040 facebookexperimental/Recoil#981 facebookexperimental/Recoil#954 I'm enticed by Jotai's garbage collection and I'm looking to convert my Recoil project to Jotai now. UPDATE: I finished migrating from Recoil to Jotai. My app was leaking megs of memory every minute with Recoil, but with Jotai it is leak-free 😀 - thank you Jotai maintainers! |
Beta Was this translation helpful? Give feedback.
-
https://twitter.com/dai_shi/status/1423486220596699142
|
Beta Was this translation helpful? Give feedback.
-
I switched to Jotai (from Recoil) for just 1 reason: Recoil has Other things like @dai-shi mentioned are like bonuses to me, they weren't the reason that I switched. Well done team! |
Beta Was this translation helpful? Give feedback.
-
I wanted to mention another MAJOR difference that is not on this list. Jotai supports "write-only" atoms whereas in Recoil, a selector needs to have both "get" and "set". This is a pretty big consideration for me to switch to Jotai. In Jotai, the "newValue" you pass to a write-only atom can be ANYTHING whereas in Recoil, the newValue you pass to the "set" portion of a selector has to be exact shape as the return value of the "get" portion! This is a huge restriction IMO... |
Beta Was this translation helpful? Give feedback.
-
Sorry, I stand corrected. It DOES support update func. |
Beta Was this translation helpful? Give feedback.
-
Another big advantage--the recoil repository is basically dead while this repo seems like it has a flourishing community. It looks like all of the FB devs who worked on recoil got fired. |
Beta Was this translation helpful? Give feedback.
-
I switched from recoil to jotai. There are some scenarios that jotai seems difficult to implement, such as
jotai refresher import { atom, Getter } from 'jotai'
export function atomWithRefresh<T>(fn: (get: Getter) => T) {
const refreshCounter = atom(0)
return atom(
(get) => {
get(refreshCounter)
return fn(get)
},
(_, set) => set(refreshCounter, (i) => i + 1),
)
} This way of writing is not suitable for progressive development. I will define a simple atom at the beginning. When I find that it needs to be refreshed, I will rewrite my atom definition. (Especially when modifying old code, we cannot well evaluate whether the two definitions are completely equivalent). Compared with recoil, I prefer the experience of recoil.
in Recoil const valueWithDefaultRemoteValue = atom({
key: 'some key',
default: selector({
key: 'some key default',
get: () => fetch('/api')
})
}) It would be very useful if this scenario could be supported, but jotai still needs to use some strange writing methods to achieve this function. in jotai const defaultValueAtom = atom(async () => {
return await fetch('/api');
})
const rawValueAtom = atom(undefined);
const valueAtom = atom((get) => {
return get(rawValueAtom) || get(defaultValueAtom);
}, (_, set, data) => {
set(rawValueAtom, data);
}) Although I can repackage it into a utility function, I need to implement this method separately in many projects. Maybe someone will implement a similar function but with a different name. In short, due to the lack of functionality, a large number of dialects will appear in jotai. Different jotai project have different usage habits, which makes maintenance difficult. |
Beta Was this translation helpful? Give feedback.
-
The last Recoil release was in April 2023. There are some questions if this library is still being maintained. |
Beta Was this translation helpful? Give feedback.
-
I have recently gotten familiar with Jotai and still searching for a concrete list of Jotai advantages over Recoil.
somehow wondering why there should be Jotai state manager with absolutely similar approach to Recoil. I'm sure maintainers are doing their best to improve the library more and more, but I wan't to get some idea about current and future of the library
Beta Was this translation helpful? Give feedback.
All reactions