Comments (31)
I am still having this issue
from react-apollo-hooks.
I have the same problem.
useQuery causes 2 renders even if the query result is already cached.
from react-apollo-hooks.
Have you tested this with environment set to production
? It might not be the reason here but React renders components that use hooks twice for dev/safety reasons when environment isn't production
.
from react-apollo-hooks.
I'm using newest "@apollo/client": "3.0.0-beta.43"
and still some additional rerenders happend. After disabling React.StrictMode works fine.
from react-apollo-hooks.
you can pass in {enabled: false}
like following and it disables extra re-rendering
useQuery('key', () => axios.get(), {enabled: false};
from react-apollo-hooks.
guys please be aware of React.StrictMode ... it will add a couple of extra renders :) ... in my case they jumped from 2 to 6 :)
from react-apollo-hooks.
useQuery renders twice for me when I use fetchPolicy: 'cache-only'
. Using 'cache-first' produces just one render.
const { data } = useQuery(GET_DATA, {
fetchPolicy: 'cache-only' //causes second render
});
from react-apollo-hooks.
This is still happening ! any one got solution to this problem ?
from react-apollo-hooks.
useQuery("Key", () => getData(), {refetchOnWindowFocus: false})
This should solve the issue.
from react-apollo-hooks.
Hello guys :) , I will leave this little help here, what I am doing is using the useApolloClient with useEffect and useState for cases when I need to do stuff with other hooks like for example a form who use a hook to load initial values, is working fine and just renders once, check:
... more imports
import React, { useEffect, useState } from 'react';
import { useApolloClient } from 'react-apollo-hooks';
const StartService = ({ id }) => {
const client = useApolloClient();
const [collections, setCollections] = useState([]);
const [service, setService] = useState({});
const [submitting, setSubmitting] = useState(false);
const [formState, { text, select }] = useFormState();
useEffect(() => {
getCollectionType();
getService();
}, []);
const getService = async () => {
if (id) {
const {
data: { getService },
} = await client.query({
query: GET_SERVICE_QUERY,
variables: { id },
});
setService(getService);
if (getService) {
formState.setField('source', getService.source || '');
formState.setField('record', getService.record || '');
formState.setField('licensePlate', getService.licensePlate || '');
}
}
};
const getCollectionType = async () => {
const {
data: { collectionsType },
} = await client.query({
query: COLLECTIONS_TYPE_QUERY,
});
setCollections(collectionsType);
};
return (...)
Renders just once, just how I like it 😉
from react-apollo-hooks.
Should be using the official Apollo react hooks by now man!
from react-apollo-hooks.
@ppsirius I'm not using Strict Mode on my app, just did a very fresh example using CRA and I still have the same problem
from react-apollo-hooks.
This issue doesn't exist on @apollo/react-hooks@beta
from react-apollo-hooks.
Same here! Not using Strict Mode. Still have 2 renders after a mutation!!!
from react-apollo-hooks.
Is this problem solved somehow?
from react-apollo-hooks.
Render with cache-data, render while loading, render with data-result = 3 times. -- That makes sense to me! How many times would you expect it to render? @asfwebmaster
from react-apollo-hooks.
Hm, I have just updated with: console.log(data, error, loading);
and the result is:
Object {dog: Object}
dog: Object
images: Array[169]
__typename: "Dog"
undefined
false
---------------------
Object {dog: Object}
dog: Object
images: Array[169]
__typename: "Dog"
undefined
false
---------------------
Object {dog: Object}
dog: Object
images: Array[169]
__typename: "Dog"
undefined
false
So the loading
part is false
and all other props are same for all renders, this seems a bit strange. @fbartho
from react-apollo-hooks.
It looks like this issue has something to do with suspense. I was able to find a limited workaround by using React.memo:
<Suspense fallback={<div>Loading...</div>}>
<MyComponent />
</Suspense>
...
const MyComponent = React.memo(() => {
const { data, error } = useQuery(QUERY)
if (error) {
return `Error! ${error.message}`;
}
return 'MyComponent'
})
In that case, MyComponent renders only once. But this seems to break if you pass children, e.g.:
<Suspense fallback={<div>Loading...</div>}>
<MyComponent>{props.children}</MyComponent>
</Suspense>
...
const MyComponent = React.memo(({ children }) => {
const { data, error } = useQuery(QUERY)
if (error) {
return `Error! ${error.message}`;
}
return children
})
from react-apollo-hooks.
Any word on this?
from react-apollo-hooks.
Have anyone found a solution or a workaround except using React.memo
which doesn't help to fully avoid the issue? This behavior makes my app very slow, because heavy components rerender multiple times without the need.
from react-apollo-hooks.
@adjourn I think you meant this behaviour. But this happens only if <React.StrictMode>
is enabled. I don't use Strict Mode and I still have lots of rerenders. So this is not the case here.
from react-apollo-hooks.
Even in production the query causes my component to re-render 4 times, which is two more than expected right? @apollo/react-hooks doesn't have this issue, but i'm having other issues that mean i still can't use that :(
from react-apollo-hooks.
use @apollo/react-hooks :)
from react-apollo-hooks.
@JClackett Are you no longer having the other issues that prevented you from using @apollo/react-hooks?
from react-apollo-hooks.
@twelve17 @apollo/react-hooks works for me now, so have been using that in all my projects
from react-apollo-hooks.
Should be using the official Apollo react hooks by now man!
Ahhh, my bad; i was so much tired to even check the package name ! :P
i am using the official one.
from react-apollo-hooks.
For me also was the problem React.StrictMode
from react-apollo-hooks.
same here
from react-apollo-hooks.
Did someone found a way to solve this problem? I'm also facing this issue.
from react-apollo-hooks.
Remove Strict Mode Hoc but is just a workaround ;)
from react-apollo-hooks.
useQuery(yourQuery, { fetchPolicy: 'no-cache' })
from react-apollo-hooks.
Related Issues (20)
- Unable to Access Result Attributes
- Calling a mutation within React.useEffect causes infinite loop HOT 9
- Error on refetch
- Apollo client not handing Throw Error networkError eg.400,500
- data is no longer defined in 0.5.0 before data is loaded HOT 2
- Archive repo
- TypeError: Object(...) is not a function
- Question: How can I abort a query already in progress? HOT 1
- useLazyQuery ignores request if one is in progress (or how to cancel running requests)
- Support for `renderToStream`
- Correct network response but incorrect data object HOT 2
- Prevent infinite loop with useQuery
- Does useQuery hook uses local state for storing the data it receives from the server ?
- useSubscription - Duplicated subscription on page refresh HOT 1
- Refetching useQuery with skip: true results in a network request but does not update data
- Testing useSubscription and no responses when other mutations being mocked
- dynamic input variables in react-apollo-hooks
- Is it possible to map the result of one query to another query? HOT 1
- Is there any way to use multiple queries before a component render inside the component itself?
- UseQuery is not calling any API in the network side, when I console.log response, I am seeing this. what could be wrong here?
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 react-apollo-hooks.