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

feature: added useSuspenseQuery hook to react package #353

Merged
merged 15 commits into from
Oct 21, 2024
Merged

Conversation

Chriztiaan
Copy link
Contributor

@Chriztiaan Chriztiaan commented Oct 15, 2024

This PR aims to introduce the useSuspenseQuery hook to the react package so that users can handle the loading state of a query with Suspense instead of the conventional isLoading/isFetching state variables.

The implementation is a minor variation of #100. The most noteworthy iterations are that it adds the suspense support under a separate hook to useQuery, it supports queries that take longer than 5 seconds to complete, and compilable queries are accepted as possible input as well.

As mentioned in the former PR, the state of the hook needs to be managed outside of the hook due to state being discarded while a component is suspended. This means that state is cached based on a query-parameters-options key which means multiple hook invocations on the same would resolve to the same query. Note that the state for a given key is disposed when the hook is destroyed, but this opens the door for potential future work to add proper query caching to both useQuery and useSuspenseQuery - with a proper interface for managing this cache (which is something that Tanstack supports really well).

A few examples have been added to the package's README.md to illustrate how to handle typical cases when using Suspense, such as blocking navigation while the target page's components are suspending.

Copy link

changeset-bot bot commented Oct 15, 2024

🦋 Changeset detected

Latest commit: 4126515

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@powersync/react Minor
@powersync/react-native Patch
@powersync/tanstack-react-query Patch
@powersync/diagnostics-app Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Chriztiaan Chriztiaan marked this pull request as ready for review October 16, 2024 06:36
Copy link
Collaborator

@DominicGBauer DominicGBauer left a comment

Choose a reason for hiding this comment

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

Can you add tests for this?

Copy link
Contributor

@rkistner rkistner left a comment

Choose a reason for hiding this comment

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

I haven't tested yet, but looks great overall

@DominicGBauer
Copy link
Collaborator

LGTM

DominicGBauer
DominicGBauer previously approved these changes Oct 16, 2024
@Chriztiaan
Copy link
Contributor Author

Chriztiaan commented Oct 17, 2024

FYI @rkistner @DominicGBauer : I made a few changes to the PR so that the WatchedQuery can extend the BaseObserver class.

packages/vue/README.md Outdated Show resolved Hide resolved
@Chriztiaan Chriztiaan merged commit 2b0466f into main Oct 21, 2024
5 checks passed
@Chriztiaan Chriztiaan deleted the react/suspense branch October 21, 2024 06:54
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.

5 participants