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

useSWR onError and onErrorRetry changed behaviour in 1.1.0 #2201

Closed
GabrielNastase opened this issue Oct 20, 2022 · 1 comment
Closed

useSWR onError and onErrorRetry changed behaviour in 1.1.0 #2201

GabrielNastase opened this issue Oct 20, 2022 · 1 comment

Comments

@GabrielNastase
Copy link

GabrielNastase commented Oct 20, 2022

Bug report

Description / Observed Behavior

We have been observed the functionality of onError and onErrorRetry callbacks has changed in the minor 1.1.0
We have cases in our apps where we call useSWR with the same cache key for multiple times, sometimes just for reading data, other times for reading data and handling error scenarios.
Before 1.1.0 ( before #1522) , we could see that all the hooks callbacks were being called back. And that was useful for when you use the same useSWR cache key multiple times in the same page. Useful for when you have multiple panels in the same page - and you want the components to get the data independently from the cache - but you would want to handle differently the error retry in each panel.
Since 1.1.0 only the first rendered hook is being called back resulting that only the first component that uses the same cache key could change the UI in case of onError is fired or onErrorRetry

Expected Behavior

If the useSWR hook has the onError and onError retry option - I would expect it to actually be called back in all the call sites.

Repro Steps / Code Example

const sameFetcher = () => Promise.reject('Ooups');

const ComponentOne = () => {
  const { data } = useSWR('same-key', sameFetcher, {
    onError: (error) => {
      console.log('ComponentOne onError fired', error) // only this is fired
    },
    onErrorRetry: (error, _key, _config) => {
      console.log('ComponentOne onErrorRetry fired', error, _key, _config) // only this is fired
    }
  })

  return <pre>{data}</pre>
}

const ComponentTwo = () => {
  const { data } = useSWR('same-key', sameFetcher, {
    onError: (error) => {
      console.log('ComponentTwo onError fired', error) // this is not called back
    },
    onErrorRetry: (error, _key, _config) => {
      console.log('ComponentTwo onErrorRetry fired', error, _key, _config) // this is not called back
    }
  })

  return <pre>{data}</pre>
}

const App = () => {

  return <div>
    <ComponentOne />
    <ComponentTwo />
  </div>
}

Additional Context

SWR version. 1.3.0

@promer94
Copy link
Collaborator

promer94 commented Jun 7, 2023

#1580

@promer94 promer94 closed this as completed Jun 7, 2023
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

No branches or pull requests

2 participants