Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

useLazyQuery #177

Merged
merged 17 commits into from
Mar 28, 2021
Merged

useLazyQuery #177

merged 17 commits into from
Mar 28, 2021

Conversation

msutkowski
Copy link
Member

@msutkowski msutkowski commented Mar 18, 2021

Introduces useLazyQuery, which allows users to opt-in to calling a query with arguments.

const [fetchThing, { data, isLoading, ...rest }] = api.endpoints.getPosts.useLazyQuery({refetchOnFocus, refetchOnReconnect, pollingInterval});

<button onClick={() => fetchThing({ with: 'variable', another: 'one' })}>Do a fetch</button>

Once a request is made, the query will stay subscribed to any changes while mounted in the scenario that there are other components mounted with the same query. If the arg that fetchThing is called with changes, it will perform a new request and then behave the same way. Additionally, we always unsubscribe an existing query before initiating a new one when the trigger is called.

[insert visual reference :D]

@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 18, 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 5599c4f:

Sandbox Source
React Configuration
React Typescript Configuration
rtk-query-demo Configuration
svelte-app-rtk-simplequery-demo Configuration

@github-actions
Copy link

github-actions bot commented Mar 18, 2021

size-limit report 📦

Path Size
ESM full 9.91 KB (+0.11% 🔺)
ESM full (React) 11.24 KB (+2.43% 🔺)
createApi + setupListeners 9.03 KB (+0.17% 🔺)
createApi (React) + setupListeners 10.24 KB (+2.66% 🔺)
fetchBaseQuery 661 B (0%)
retry 271 B (0%)
ApiProvider 400 B (0%)
CJS minfied 15.37 KB (+0.07% 🔺)
CJS React minfied 16.94 KB (+2.04% 🔺)

@phryneas phryneas added this to the 0.3.0 milestone Mar 19, 2021
@msutkowski
Copy link
Member Author

Move arg into the hook call site.

@msutkowski msutkowski force-pushed the lazy-query branch 4 times, most recently from 5692070 to b097568 Compare March 20, 2021 22:16
@msutkowski msutkowski marked this pull request as ready for review March 20, 2021 22:32
@msutkowski
Copy link
Member Author

msutkowski commented Mar 20, 2021

Note: there is a bug in this. When the input args on a call get changed, we're currently leaving stale queries around. Need to clean this up.

src/react-hooks/buildHooks.ts Outdated Show resolved Hide resolved
src/react-hooks/buildHooks.ts Outdated Show resolved Hide resolved
src/react-hooks/buildHooks.ts Outdated Show resolved Hide resolved
@msutkowski msutkowski force-pushed the lazy-query branch 2 times, most recently from bd9822a to 57a2bee Compare March 21, 2021 17:16
@SaulMoro SaulMoro mentioned this pull request Mar 21, 2021
3 tasks
@msutkowski msutkowski force-pushed the lazy-query branch 8 times, most recently from 25d1a65 to 4771d0a Compare March 22, 2021 01:03
@msutkowski
Copy link
Member Author

msutkowski commented Mar 22, 2021

Okay, remaining issues with this:

  1. doing a forceUpdate in the ref as tried in this commit might be cleanest, but not sure: 53ed749
  2. TS41Hooks need to be updated for useLazy${thing}Query?
  3. fast refresh behavior isn't really working well with this approach, but i don't think there is a really good solution. being that we unsubscribe on unmount, we don't really have a good way to know thats whats happening so a user would have to refire the query. maybe i'm missing something tho

@msutkowski msutkowski force-pushed the lazy-query branch 2 times, most recently from 07cfc1e to 7176c0b Compare March 25, 2021 17:00
Copy link
Member Author

@msutkowski msutkowski left a comment

Choose a reason for hiding this comment

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

I think this looks good. I agree with everyone who says hooks can be hard :)

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

Successfully merging this pull request may close these issues.

2 participants