Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

atomsWithQuery wrap by atom(async) will trigger unnecessary loading #50

Open
Landon-CN opened this issue Oct 7, 2023 · 6 comments
Open

Comments

@Landon-CN
Copy link

code example: https://codesandbox.io/p/sandbox/small-brook-7vmvwd?file=%2Fsrc%2FApp.tsx%3A32%2C27

first use fetch data directly
const data = useAtomValue(userAtom);
the console will not has loading
image

two wrap by atom(async)
const data = useAtomValue(asyncToJsonAtom);
the console will has loading
image

in example two, even if use query cache data, the loading will show again
so screen will flash: content->loaindg->content. because time is short

expect:
not trigger loading

@dai-shi
Copy link
Member

dai-shi commented Oct 7, 2023

Interesting. It seems like a bug, but I don't have any idea how it happens.
Thanks for reporting. Hope someone can investigate it further.

@Landon-CN
Copy link
Author

Interesting. It seems like a bug, but I don't have any idea how it happens. Thanks for reporting. Hope someone can investigate it further.

i try to debug it. in example one whe use cache,it will not use promise https://github.com/jotaijs/jotai-tanstack-query/blob/main/src/common.ts#L331 ,will triiger like sync atom.
but if you wrap with atom(async) . useAtomValue will treat it as promiseLike https://github.com/pmndrs/jotai/blob/main/src/react/useAtomValue.ts#L106
so it will trigger suspense

@dai-shi
Copy link
Member

dai-shi commented Oct 8, 2023

I would like to make sure if it's a jotai-tanstack-query issue. If it can be reproduced with bare Jotai, it's more unexpected.

@Landon-CN
Copy link
Author

I would like to make sure if it's a jotai-tanstack-query issue. If it can be reproduced with bare Jotai, it's more unexpected.

i try anthor example, async wrap with a new query
image
i think this bug is caused by:

  1. const data = useAtomValue(asyncToJsonAtom); throw Promise. block blow useAtomValue(asyncTwiceAtom)
  2. promise resloved. execute useAtomValue(asyncTwiceAtom), throw promise
  3. promise resloved rendering

tanstack query solve this case is use https://tanstack.com/query/v5/docs/react/reference/useSuspenseQueries

https://codesandbox.io/p/sandbox/elated-dust-57sk3m?file=%2Fsrc%2FApp.tsx%3A42%2C25

@dai-shi
Copy link
Member

dai-shi commented Oct 13, 2023

Does Promise.all help?
ref: https://twitter.com/dai_shi/status/1699553010245067022

@Landon-CN
Copy link
Author

it try to combie two async atom -> one async atom

const combineAtom = atom(async (get) => {
  const data1 = await get(asyncToJsonAtom);
  const data2 = await get(asyncTwiceAtom);

  return [data1, data2];
});

image

Seems to have solved the problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants