selectAtom not working any more #1988
Replies: 14 comments 51 replies
-
Please provide a minimal reproduction, preferably with https://csb.jotai.org. |
Beta Was this translation helpful? Give feedback.
-
If this is an issue with async behavior, I assume it can be reproduced without selectAtom. |
Beta Was this translation helpful? Give feedback.
-
On second thought, I'm converting this to a discussion, because #1158 is a closed issue. |
Beta Was this translation helpful? Give feedback.
-
@joacub Can you summarize again what you have found in #1158? |
Beta Was this translation helpful? Give feedback.
-
yes let me explain, i already investigate and i saw the issue, there were a really breaking change in the latest version about async behavior. in my next post i will explain you whaat is happening. |
Beta Was this translation helpful? Give feedback.
-
When I use
Without this wrapper function, |
Beta Was this translation helpful? Give feedback.
-
It appears that there hasn't been any change since the update regarding asynchronous operations, and I suspect that the issue lies in that area. The code remains broken because it can break at any time when the selector returns a promise. However, it seemed to be working before, possibly due to luck. The problem seems to be related to the changes made in the atomWithStorage function, although the issue was present even before these changes were implemented. Let me explain the situation. We have a store specification where the store should always return a value, regardless of what that value is. However, when we use the selectAtom function and it returns an error instead of the current state, it clearly goes against the desired behavior of a storage. The selector should always resolve to the current state, irrespective of whether it is an asynchronous storage or not. We want to access the current state, not the asynchronous function that will eventually return the value. It's important to note that React hooks do not allow for asynchronous operations. To address this, we are using an internal state that has its own storage to access unmounted data stored within it. Essentially, the storage is not functioning correctly as the first action always returns nothing. |
Beta Was this translation helpful? Give feedback.
-
https://codesandbox.io/s/quizzical-haslett-tdqph2?file=/src/useCache.ts you can see here, this example is not triggering anything, not even the update. so that's maybe the reason is not been a loop. |
Beta Was this translation helpful? Give feedback.
-
It appears that the issue I'm encountering doesn't occur when running the code in a browser using Jot. In the browser environment, React-Dom gracefully handles the error with suspense, ensuring smooth operation. However, in React Native, the situation is different. Consequently, the problem persists and keeps repeating at that specific point. |
Beta Was this translation helpful? Give feedback.
-
You can see in the logs, that the promise error happens at the beginning, in react native when that happen all the hooks gets executed again without pass to the next code block. every time that get unmount and mount the promise like error happens. That not the desired behavior. Anyways the promise like error shouldn’t happen never. As I said the hook should return the current state always. |
Beta Was this translation helpful? Give feedback.
-
This si the issue in the last update you are always doing the atom asynchronous: jotai/src/vanilla/utils/atomWithStorage.ts Line 130 in aa9859f jotai/src/vanilla/utils/atomWithStorage.ts Line 131 in aa9859f this is the old approach and the new one assign the asynchronous get item to the atom And here you can see that you are telling the atom it is asynchronous when is not or does not have to be. jotai/src/vanilla/utils/atomWithStorage.ts Line 157 in f4adf6c |
Beta Was this translation helpful? Give feedback.
-
I'm trying to reproduce the behavior with So far, here's what I did. |
Beta Was this translation helpful? Give feedback.
-
Opened 4 PRs: #1994 #1995 #1996 #1997 |
Beta Was this translation helpful? Give feedback.
-
Hi! Sorry if I missed anything (the discussion is quite long at this point and seems to have started to look into storage instead), but here's a very minimal reproduction: https://codesandbox.io/p/sandbox/dh56hx. The basic idea is that if
|
Beta Was this translation helpful? Give feedback.
-
Summary
selectAtom is returning for async atoms always a promise which cause a hooks to return a throw (promise), we expect that the hook just return the state if the state is not ready then do not return the state and set the sate later.
but now it is imposible to use the hook as this always return an promise like error and never update the state.
Beta Was this translation helpful? Give feedback.
All reactions