question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validation retriggers when component is mounted again.

See original GitHub issue

Bug report

Suppose I need to fetch data with a POST request, where I have to send the ids in the request body.

const MyComponent = (props) => {
    const body = useMemo( () => ({ ids: props.ids }), []) 
    const { data } = useSWR(['/api', body])

    return //...
}

This works fine, the problem is that when I go to another page and then get back to the first one (containing MyComponent) the request is retriggered. This makes sense since useMemo only caches the data as long as the component is mounted. But is there a way to avoid retriggering the request when mounting the component again?

Thanks in advance,

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

30reactions
Ashot-KRcommented, Sep 3, 2020

You can set revalidateOnMount as false in the hook options. But ideally you use SWR only for GET requests, because the idea of SWR is that it will revalidate in many cases to ensure data consistency

with revalidateOnMount: false swr doesn’t make request even when there is no cached data yet. with revalidateOnMount: true request will be send even if cached date exists. I think option revalidateOnCacheExists: false(or something like this) will be useful when we need to display cached date without any data requests.

16reactions
nikhilagcommented, Oct 16, 2020

What’s the reason for not fetching data if it doesn’t exist in cache when we set revalidateOnMount: false?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't perform a React state update on an unmounted ...
It stops the repetitive call of setState method because it validate _isMounted value before setState call, then at last again reset to false ......
Read more >
React - How to Check if a Component is Mounted or Unmounted
This is a quick post to show how to track the mounted status of a React component so you can check if it's...
Read more >
useForm - trigger - Simple React forms validation
Manually triggers form or input validation. This method is also useful when you have dependant validation (input validation depends on another input's ...
Read more >
How To Handle Async Data Loading, Lazy Loading, and Code ...
If you do not include an array of triggers, it will run after every render. Open RiverInformation.js : nano src/components/RiverInformation/ ...
Read more >
Validation Observer - VeeValidate
Here is a small example, again with Vuetify components wrapped by the Provider's withValidation ... A method that triggers validation for all providers....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found