Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

onError still getting called multiple times in v3.0.1 #3407

Closed
dylanwulf opened this issue Aug 23, 2019 · 1 comment · Fixed by #3419
Closed

onError still getting called multiple times in v3.0.1 #3407

dylanwulf opened this issue Aug 23, 2019 · 1 comment · Fixed by #3419

Comments

@dylanwulf
Copy link
Contributor

dylanwulf commented Aug 23, 2019

Intended outcome:

onError should be called once per failed request

Actual outcome:

onError is called multiple times

How to reproduce the issue:
https://codesandbox.io/s/onerror-gets-called-many-times-v301-nzucv

This was supposed to be fixed by #3339, but I am still seeing onError get called multiple times for a single error.

image

Version
@apollo/react-components: 3.0.1
@apollo/react-hooks: 3.0.1
apollo-cache-inmemory: 1.6.3
apollo-client: 2.6.4
apollo-link-http: 1.5.15

@dylanwulf
Copy link
Contributor Author

dylanwulf commented Aug 26, 2019

Update: I found that the problem only occurs if onError or onCompleted are inline functions e.g.
onError={err => console.log(err)}

If onError and onCompleted are class instance methods, then the problem does not happen:

class Test extends React.Component {
    handleError = err => {
        console.log(err);
    };
    
    render() {
        <Query onError={this.handleError}>...</Query>
    }
}

https://codesandbox.io/s/onerror-gets-called-once-with-class-instance-method-l620v

I'm guessing this has to do with the fact that inline functions are recreated on every render.
This makes it very easy to work around the issue if you are using class components. If you are using a functional component, you could possibly get around the issue by memoizing your onError and onCompleted functions.

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 a pull request may close this issue.

1 participant