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

useQuery's error object disappears on rerender #3295

Closed
hartbeatnt opened this issue Jul 31, 2019 · 13 comments · Fixed by #3362
Closed

useQuery's error object disappears on rerender #3295

hartbeatnt opened this issue Jul 31, 2019 · 13 comments · Fixed by #3362
Assignees
Labels
has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository

Comments

@hartbeatnt
Copy link

Intended outcome:

Error object for failed query should persist across rerenders

Actual outcome:

The error object disappears after rerendering

How to reproduce the issue:

  • have the @apollo/react-hooks branch installed
  • have a proxy set up to force a request to fail. Charles is a good option: https://www.charlesproxy.com/
  • The ParentComponent has a 5 second timer. Force the request for your query to fail before that timer expires. It will show the there is an error message. But once the timer expires and the Parent rerenders the child component, the error object will disappear and it will display "there is no error" without refiring the query
const ReproComponent = () => {
  const { error } = useQuery(/* insert your gql query here */)
  if (error) {
    return <div> there is an error </div>
  }
  return <div> there is no error </div>
}

class ParentComponent {
  constructor() {
    super()
    this.state = { count: 0 };
    this.increment = this.increment.bind(this)
    setTimeout(this.increment, 5000)
  }

  increment() {
    this.setState(prevState => ({ count: prevState.count + 1 })
  }

  render() {
    return <ReproComponent count={ this.state.count } />
  }
}

Version

System:
OS: macOS 10.14.5
Binaries:
Node: 8.12.0 - /usr/local/bin/node
npm: 6.5.0 - ~/.npm-packages/bin/npm
Browsers:
Chrome: 75.0.3770.142
Safari: 12.1.1
npmPackages:
apollo-cache-inmemory: ^1.1.7 => 1.6.2
apollo-cache-persist: ^0.1.1 => 0.1.1
apollo-client: ^2.2.3 => 2.6.3
apollo-link: ^1.1.0 => 1.2.12
apollo-link-context: ^1.0.8 => 1.0.18
apollo-link-error: ^1.1.0 => 1.1.11
apollo-link-http: ^1.5.2 => 1.5.15
apollo-link-rest: ^0.2.4 => 0.2.4
apollo-link-retry: ^2.2.4 => 2.2.14
apollo-link-state: ^0.4.0 => 0.4.2
apollo-upload-client: ^8.1.0 => 8.1.0
react-apollo: ^2.1.4 => 2.5.8

=> also @apollo/react-hooks, but that doesn't log from the npx command

@hwillson hwillson added this to the Release 3.0 milestone Jul 31, 2019
@hwillson hwillson self-assigned this Jul 31, 2019
@jasonpaulos
Copy link
Contributor

Hi @hartbeatnt, what version of @apollo/react-hooks are you using? I believe this should not be an issue anymore in 0.1.0-beta.11, but if it is please let us know!

@hartbeatnt
Copy link
Author

Hi @hartbeatnt, what version of @apollo/react-hooks are you using? I believe this should not be an issue anymore in 0.1.0-beta.11, but if it is please let us know!

I am using 0.1.0-beta.11

@hwillson
Copy link
Member

hwillson commented Aug 5, 2019

@hartbeatnt Can you try with @apollo/react-hooks@0.1.0-beta.12 to see if that helps? If not, would you mind putting together a small runnable reproduction?

@hwillson hwillson removed this from the Release 3.0 milestone Aug 6, 2019
@Geit
Copy link

Geit commented Aug 7, 2019

Hi @hwillson - We're seeing this (or, at least, a similar issue) intermittently manifest in tests using MockedProvider.

I've put together a reproduction here: https://github.com/Geit/apollo-error-rerender-repro

@hwillson
Copy link
Member

hwillson commented Aug 7, 2019

Thanks for the repro @Geit - I'll take a look!

@hwillson hwillson added has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository and removed reproduction-needed labels Aug 7, 2019
@moritzmhmk
Copy link

I do currently work around this bug by using if (error || Object.entries(data).length === 0), any progress on fixing this?

hwillson added a commit that referenced this issue Aug 13, 2019
Issue #3295 demonstrates a problem where errors aren't maintained
on re-renders, when they should still be available. This issue
was fixed by PR #3339, but this commit will add an extra test to
verify that the issue is indeed fixed.

Fixes #3295.
@hwillson
Copy link
Member

This issue was fixed by #3339, which will be included in the next release. Thanks all!

hwillson added a commit that referenced this issue Aug 13, 2019
…3362)

* Add test to verify error is persisted on re-render, when appropriate

Issue #3295 demonstrates a problem where errors aren't maintained
on re-renders, when they should still be available. This issue
was fixed by PR #3339, but this commit will add an extra test to
verify that the issue is indeed fixed.

Fixes #3295.

* Changelog update
@dylanwulf
Copy link
Contributor

dylanwulf commented Aug 21, 2019

@hwillson I am still experiencing this problem with v3.0.1 when using the Query component, here is a reproduction: https://codesandbox.io/s/wdu8j
The error object always disappears on the next render, and occasionally onError will get called twice.

@kartikjain26
Copy link

kartikjain26 commented Jan 30, 2020

@dylanwulf @hwillson I am experiencing this error on using useQuery. error property is undefined after initial render.

Can you re open this issue ?

"@apollo/react-hooks": "3.1.3",
"react-apollo": "2.5.8",

@dylanwulf
Copy link
Contributor

dylanwulf commented Jan 30, 2020 via email

@dylanwulf
Copy link
Contributor

@kartikjain26 Just noticed the package versions you posted. You should not be using @apollo/react-hooks and react-apollo at the same time. You are probably seeing this issue because you are using an old version of react-apollo.

@kartikjain26
Copy link

@dylanwulf This is occurring once in 30-40 times I try. Will post a recording or Screenshot if I manage to capture it.
Also, why should not we use @apollo/react-hooks and react-apollo at the same time ?

@dylanwulf
Copy link
Contributor

dylanwulf commented Jan 31, 2020 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants