Async atom that suspends the first time, returns previous value subsquently #2228
-
My team is trying to create an async atom that suspends the first time while the initial value is being loaded but subsequently returns the previous value synchronously when new values are being loaded. For example: const values = ["first-value", "second-value"];
const syncAtom = atom(0);
const swrAtom = selectAtomWithSwr(async (get) => values[get(syncAtom) % 2]); We want Here is a CodeSandbox demonstrating what we are trying (but failing) to do. The Suspense loading fallback prints a message every time it renders. We want to see that message just once upon initial load, but we are seeing it every time the state changes and a new async request is made: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
It's tricky, but should be possible. Maybe, use |
Beta Was this translation helpful? Give feedback.
Thanks
unwrap
as you described did the trick! It's much simpler now, and works:Here's the important part: