[^3.0.0] data is undefined while loading
See original GitHub issueIntended outcome:
Let’s take the example from your website:
const GET_DOG_PHOTO = gql`
query Dog($breed: String!) {
dog(breed: $breed) {
id
displayImage
}
}
`;
function DogPhoto({ breed }) {
const { loading, error, data } = useQuery(GET_DOG_PHOTO, {
variables: { breed },
});
if (loading) return null;
if (error) return `Error! ${error}`;
return (
<img src={data.dog.displayImage} style={{ height: 100, width: 100 }} />
);
}
when the breed
variable changes, it should set loading = true
and return the past data
until it resolves.
Actual outcome:
when the breed
variable changes, it sets loading = true
and returns undefined
for data
until it resolves.
How to reproduce the issue:
See the example above. This used to work in Apollo 2.x. I think it has to do with the cache, but there is nothing mentioned on the docs. This is indeed an app-breaking bug.
Versions
Issue Analytics
- State:
- Created 3 years ago
- Reactions:13
- Comments:12 (2 by maintainers)
Top Results From Across the Web
Cannot read property '0' of undefined - Stack Overflow
1 Answer 1 · The code as it is actually works for me, I think the problem is with jquery library not loaded...
Read more >How To Fix Cannot read Property '0' of Undefined in JS
This is a decent solution because it will guarantee that you do not get the error at runtime which might cause problems elsewhere...
Read more >API — Jinja Documentation (3.0.x)
Modifications on environments after the first template was loaded will lead to surprising effects and undefined behavior. Here are the possible ...
Read more >Why does Apollo client's useQuery return undefined data?
... but data and error return undefined. I am using \@apollo/client version 3.0.0-beta.24 ... While it's loading, there's no data or error.
Read more >How to catch a javascript error (cannot read property 0 of ...
In your case, the error you have mentioned will come if the Object is undefined, not when you try to access an non...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m a bit late to the discussion but I just wanted to mention this change broke the UI for multiple of my apps and I didn’t notice the issue for a while… If it was changed on purpose, it should certainly have been highlighted in huge red type in the Apollo Client release notes or migration guide!
@mrtnbroder This was an intentional change introduced in AC3 by #6566 to fix #6039, and listed as a [BREAKING] change in
CHANGELOG.md
:loading: true
will no longer redeliver previous data, though they may provide partial data from the cache, when available. @benjamn in #6566It’s fair to say this hasn’t been a popular change, but the only way I can see to restore reasonable behavior (so that #6039 remains fixed) would be to provide
result.previousData
separately fromresult.data
, as I proposed in https://github.com/apollographql/apollo-client/issues/6603#issuecomment-658803488.The old behavior was ambiguous, because
result.data
could either be a cache result for the new variables, or a stale result for some older/different variables. If we’re going to be providing stale data when variables change, it needs to come clearly labeled (e.g.result.previousData
).