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

revalidateOnMount flag that disable automatic revalidation on mount #225

Merged
merged 10 commits into from May 12, 2020

Conversation

ciak0
Copy link
Contributor

@ciak0 ciak0 commented Jan 10, 2020

Hi there,
we're currently using useSWR but we have found conditional revalidation a bit limiting.

For example we have some occurences where we want to trigger revalidation only after a certain action is being taken (could be a button click).
With current conditional revalidation techniques we are forced to use an additional state just to prevent first validation on mount.

This PR aims to add a simple autoLoad (or whatever other name) flag in the options so that automatic revalidation on mount can be prevented and we can simply trigger it directly in a callback (for example a button onClick).

Best,
Gabriele

@ciak0 ciak0 changed the title autoLoad flags that disable automatic revalidation on mount [WIP] autoLoad flags that disable automatic revalidation on mount Jan 10, 2020
@ciak0 ciak0 changed the title [WIP] autoLoad flags that disable automatic revalidation on mount autoLoad flag that disable automatic revalidation on mount Jan 10, 2020
src/use-swr.ts Outdated Show resolved Hide resolved
test/use-swr.test.tsx Outdated Show resolved Hide resolved
@sergiodxa
Copy link
Contributor

Could I propose rename from autoLoad to revalidateInitialData?

@ciak0
Copy link
Contributor Author

ciak0 commented Jan 28, 2020

Ok, will do it!

@ciak0 ciak0 changed the title autoLoad flag that disable automatic revalidation on mount revalidateInitialData flag that disable automatic revalidation on mount Jan 28, 2020
@ciak0
Copy link
Contributor Author

ciak0 commented Jan 28, 2020

Could I propose rename from autoLoad to revalidateInitialData?

Just pushed option with new name and updated documentation README that I previously forgot.
Let me know what you think about it @sergiodxa @quietshu!

Cheers

Copy link
Contributor

@sergiodxa sergiodxa left a comment

Choose a reason for hiding this comment

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

Based on the test case, should this flag prevent SWR to revalidate the data provided as initialData only or also preven the revalidation if there is an already cached value for the given key?

If it's the last, then ignore my comments in the code and I would propose to use revalidateOnMount as name to describe better what this does.

README.md Outdated Show resolved Hide resolved
src/use-swr.ts Outdated Show resolved Hide resolved
@ciak0
Copy link
Contributor Author

ciak0 commented Jan 28, 2020 via email

@ciak0 ciak0 changed the title revalidateInitialData flag that disable automatic revalidation on mount revalidateOnMount flag that disable automatic revalidation on mount Feb 18, 2020
@ciak0
Copy link
Contributor Author

ciak0 commented Feb 18, 2020

@sergiodxa @quietshu sorry for the delay, just updated PR with suggested hints and made it more clear. Let me know if you think is good or you have any other comment!

Best,
Gabriele

@ciak0 ciak0 requested a review from shuding April 1, 2020 09:11
src/use-swr.ts Outdated Show resolved Hide resolved
@ciak0
Copy link
Contributor Author

ciak0 commented May 5, 2020

@shuding @pacocoursey @Timer any news? do I have to do something else?

@sergiodxa
Copy link
Contributor

@shuding is there something else @ciak0 needs to do to merge this?

Copy link
Member

@shuding shuding left a comment

Choose a reason for hiding this comment

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

@sergiodxa @ciak0 sorry for the delay!

The code looks good to me. Just one small request: let’s remove the example from the README for now (but keep the config description). I think it’s a great opportunity to create a helper hook on top of it:

{ mutate } = useMutation(key, fetcher)

// equivalent to 
useSWR(key, fetcher, {
  revalidateOnMount: false,
  revalidateOnFocus: false,
  revalidateOnConnect: false,
  // ...also disable error retry and interval
})

What do you think?

@shuding
Copy link
Member

shuding commented May 8, 2020

...although there’re some other things we need to figure out for useMutation, but that’s another topic.

Anyway, I think generally it’s great for useSWR to have a lot of options and be very customizable, but it’s better to not expose them directly to the users and have some helper abstractions. An example is $.post$.ajax.

@sergiodxa
Copy link
Contributor

👍 I think it makes sense, I was thinking that this would allow us to have a useSWRLazy which will only trigger the fetch after you run mutate, but could still read from cache if it was already cached.

@ciak0
Copy link
Contributor Author

ciak0 commented May 9, 2020 via email

@sergiodxa
Copy link
Contributor

@shuding I think this is ready to merge?

Copy link
Member

@shuding shuding left a comment

Choose a reason for hiding this comment

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

Thank you for your awesome work!

@shuding shuding merged commit cd095dd into vercel:master May 12, 2020
@ciak0
Copy link
Contributor Author

ciak0 commented May 12, 2020 via email

@j-xzy
Copy link

j-xzy commented Sep 16, 2020

revalidateOnMount set false, will prevent subsequent conditional fetching. @ciak0
I think "OnMount" is't a good name @sergiodxa

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.

None yet

5 participants