Comments (9)
Yeah, weird. Let me try to reproduce the issue in a codesandbox. If I'm doing something wrong I'll probably find out then. I'll keep you posted.
from graphql-react.
Thanks for your quick reply.
internally two useState values are being set, one immediately after the other, which in React can result in two very fast "renders"
Actually as you can see in this codesandbox example, that's not the case: https://codesandbox.io/s/green-architecture-023dc
Doing 2 following setState
still will trigger 1 single render with both setState
applied at the same time (not one after the other).
Anyway I will try to have a look at the source code of the useGraphQL
see if I find something. Good luck with your new contract!
from graphql-react.
I'm not sure why at first you're getting loading: false
, since in the tests that does not happen:
graphql-react/src/test/useGraphQL.mjs
Line 75 in 1b1234d
from graphql-react.
@jaydenseric I was able to reproduce it in this codesandbox.
from graphql-react.
Bear with me, but I think this may be working as intended.
It's been a while since I dove deep into React hooks, exploring all the gotchas (console.log
debugging hooks is confusing), but here are my rusty thoughts…
The first log of loading: false
is correct; because that state only updates once it actually starts loading, which is not until after it mounts. The first console.log
can be from a render that happened before the useEffect
triggers loading. Note that in the situation where the exact same query is already loading in the client (triggered by something somewhere else) at the time the component mounts, the first log may be loading: true
.
Now my memory may be hazy/wrong, but when multiple useState
values are updated internally there can be multiple re-renders, but you don't actually see each re-render happen in the real DOM if they happen quickly.
You do need to make sure you nest any JSX using cacheValue.data
in a conditional that checks data
actually exists first.
from graphql-react.
Closing because I'm pretty sure everything is working as intended; the general rule is to account for cacheValue
sometimes not being defined, as documented in all the useGraphQL
examples:
const { loading, cacheValue = {} } = useGraphQL({
// …
})
https://github.com/jaydenseric/graphql-react/tree/v9.1.0#examples-8
I always do cacheValue = {}
, and haven't had any problems in all my apps.
It's potentially useful to be able to tell when rendering if there is no cache for a particular query, so it's probably good to leave the API the way it is and not default cacheValue
to an empty object.
from graphql-react.
I'm having the same issue and I did a reproductible example with the doc snippet fetching a Pokemon: https://codesandbox.io/s/gracious-leakey-hisdk
As described by @olistic there is a state that imo should not happen:
-
loading starts:
{ loading: true, data: undefined }
-
loading is finished but
data
is stillundefined
??{ loading: false, data: undefined }
This state does not make sense because it then trigger theif
condition displaying "Error!
" in the CodeSandbox (it's not visible on the example since it's instantly removed). -
One tick later, finally the
data
is there:{ loading: false, data: {…} }
As you can see in that CodeSandbox I don't think there is any external hook hijacking the render loop in the middle that could cause that. So it seems to come from graphql-react
.
In my app this is actually causing an issue since I'm displaying an error message with a transition animation so even if the wrong state is really short, it stills trigger the in/out animation creating a flash of the error state.
from graphql-react.
@Grsmto are you using console.log
to debug the hook return value? A quick guess is that internally two useState
values are being set, one immediately after the other, which in React can result in two very fast "renders", but only one change to the DOM.
I plan to look into how these lifecycle gotchas can be improved when I start work on the next version; I've been working hard on other projects lately (several new testing related packages, and graphql-upload
v10), and now I've got full time contract work to contend with.
from graphql-react.
This should be improved in v10.0.0
from graphql-react.
Related Issues (20)
- hello can you convert for native js? HOT 6
- Document the `load` function HOT 3
- Support automatic persisted queries HOT 2
- How should you implement auth using this library? HOT 8
- Possibly missing files within package HOT 2
- Add ability to load on mount only if no cache HOT 2
- How to move gql query to useGraphQL operation query HOT 1
- Publish the GraphQL client in a separate package HOT 5
- Wrong type calculation on useGraphQL HOT 1
- How to use useGraphQL hooks when handleLoadMore functions trigger? HOT 1
- Allow React component displayName and propTypes to be removed in production builds HOT 3
- Pagination possible? HOT 4
- New composable React hooks HOT 2
- Add a license file HOT 3
- Add ability to clear not currently subscribed cache
- Switch to user defined cache keys
- Publish SSR functionality in a separate package
- error - unhandledRejection: TypeError: Head.rewind is not a function -> can't get the official example to work HOT 3
- How to use useLoading() to get the old loading from useGraphql()? HOT 4
- Clear code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-react.