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

feat(query): atomWithQuery to use options.initialData to avoid suspense #548

Merged
merged 4 commits into from
Jun 24, 2021

Conversation

aulneau
Copy link
Collaborator

@aulneau aulneau commented Jun 23, 2021

This updates the initial atom used for data for the query atom to be atomWithDefault and uses the initialData value if present for that. This is helpful because if data is passed, the query atom will not be suspense/async by default. This is very helpful in the context of SSR where suspense is not well supported yet.

@dai-shi curious what you think, I've tested this locally and it works well. The last thing I'd like to do is pass get to the initialData function, what do you think?

Also this type error I am not sure how to fix yet:

src/query/atomWithQuery.ts(42,23): error TS2349: This expression is not callable.
  Not all constituents of type 'InitialDataFunction<TQueryData> | (TQueryData & Function)' are callable.
    Type 'TQueryData & Function' has no call signatures.

@vercel
Copy link

vercel bot commented Jun 23, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pmndrs/jotai/J5Jh72RbZ5PLb7KHWvKG4Y6WHEca
✅ Preview: https://jotai-git-fork-aulneau-feat-query-use-default-pmndrs.vercel.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 23, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit a7eb6bc:

Sandbox Source
React Configuration
React Typescript Configuration

@dai-shi
Copy link
Member

dai-shi commented Jun 24, 2021

error TS2349: This expression is not callable.

Currently, it's TS limitation. microsoft/TypeScript#37663

@dai-shi
Copy link
Member

dai-shi commented Jun 24, 2021

@aulneau Pushed a commit.

Note that this breaks the note in the official docs.
https://react-query.tanstack.com/reference/useQuery

If set to a function, the function will be called once during the shared/root query initialization, and be expected to synchronously return the initialData

It will be twice.

@dai-shi dai-shi changed the title feat(query): use atomWithDefault and initialData feat(query): atomWithQuery to use options.initialData to avoid suspense Jun 24, 2021
@aulneau
Copy link
Collaborator Author

aulneau commented Jun 24, 2021

Nice, just tested this out and works the same as what I had.

For my own curiosity, why did you prefer to do this over the atomWithDefault?

@dai-shi
Copy link
Member

dai-shi commented Jun 24, 2021

atomWithDefault has two atoms internally (which overlaps with dataAtom here), and does potentially extra work.

Furthermore, the grand rule of mine is jotai/* only depends on jotai. Something more complex should be out of the scope. They would be 3rd party libs or recipes in docs.

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are fine with breaking the official note. It's OK for me.

@aulneau
Copy link
Collaborator Author

aulneau commented Jun 24, 2021

Ah makes sense!

I'm fine with it, I personally have never used the function version of initial data. I'd be curious to see how it might be problematic if it fires twice. Let's cross that bridge when or if we come to it.

Thanks for updating the implementation!

@aulneau
Copy link
Collaborator Author

aulneau commented Jun 24, 2021

@dai-shi would a merge be possible soon? Thanks 🙏

@dai-shi dai-shi merged commit fb38337 into pmndrs:master Jun 24, 2021
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

Successfully merging this pull request may close these issues.

2 participants