Replies: 1 comment
-
Is your idea close to "a promise value"? https://blog.axlight.com/posts/jotai-tips/#:~:text=Tip%203%3A%20Promise%20value As you found another post, this is by design. We provide primitive utils in |
Beta Was this translation helpful? Give feedback.
-
Hi everyone,
I currently use Jotai without React in my project, and I have some new ideas regarding Loadable Atoms.
According to the documentation found at Async Guide and Async Utilities, Jotai allows us to retrieve the state of an Async Read Atom easily:
However, it seems
loadable
cannot be applied to writable atoms, particularly during an async set process. This is the issue I aim to discuss here. I’m not entirely sure my thoughts are correct, but I haven’t found better answers from the community, so I hope to engage in a discussion about it.Why We Need Async Writable Atoms
Async readable atoms cannot accept parameters; all parameters must be passed through atoms. For example:
This can lead to a significant increase in the number of atoms, as any parameter needed in a
get
method requires its atom:Moreover, encapsulating these parameter atoms' visibility seems challenging, as any code can set these atoms from anywhere.
Additionally, the code for setting an atom and initiating a request appears unrelated. For example, the two lines of code below seem completely disconnected:
This leads us into a "global variable hell."
Adding Support for Writable Atoms in Loadable
It would be more intuitive for the parameters required for an asynchronous request to be passed via function arguments, similar to how writable atoms work:
Now, externally, we can obtain the result using
store.set
:Naturally, we would like to display a loading state during the request process (since we're not using React, we don't have Suspense). This can be implemented using
loadable
, leveraging temporary internal atoms.Ultimately, we obtain a read & write atom whose read method returns the status of the last fetch:
The construction of
articlesAtom
can be abstracted into a higher-order function:So far, this is my attempt at creating a loadable writable atom. When I finished this issue, I found another post: #1061 (reply in thread); the pattern is very similar. So
I wonder whether the
loadable
method could inherently support wrapping writable atoms to implement this pattern.Beta Was this translation helpful? Give feedback.
All reactions