Coder Social home page Coder Social logo

Comments (31)

dubcs avatar dubcs commented on August 16, 2024 22

I am still having this issue

from react-apollo-hooks.

Alexandredc avatar Alexandredc commented on August 16, 2024 14

I have the same problem.
useQuery causes 2 renders even if the query result is already cached.

from react-apollo-hooks.

adjourn avatar adjourn commented on August 16, 2024 12

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.

ppsirius avatar ppsirius commented on August 16, 2024 11

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.

SafiUllahAshfaq avatar SafiUllahAshfaq commented on August 16, 2024 10

you can pass in {enabled: false} like following and it disables extra re-rendering
useQuery('key', () => axios.get(), {enabled: false};

from react-apollo-hooks.

rares-lupascu avatar rares-lupascu commented on August 16, 2024 8

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.

robertvorthman avatar robertvorthman commented on August 16, 2024 8

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.

SyedAsimAliSE avatar SyedAsimAliSE commented on August 16, 2024 5

This is still happening ! any one got solution to this problem ?

from react-apollo-hooks.

imadldn avatar imadldn commented on August 16, 2024 4

useQuery("Key", () => getData(), {refetchOnWindowFocus: false})
This should solve the issue.

from react-apollo-hooks.

hipdev avatar hipdev commented on August 16, 2024 3

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.

JClackett avatar JClackett commented on August 16, 2024 3

Should be using the official Apollo react hooks by now man!

from react-apollo-hooks.

clovisdasilvaneto avatar clovisdasilvaneto commented on August 16, 2024 3

@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.

Alexandredc avatar Alexandredc commented on August 16, 2024 2

This issue doesn't exist on @apollo/react-hooks@beta

from react-apollo-hooks.

amkazan avatar amkazan commented on August 16, 2024 2

Same here! Not using Strict Mode. Still have 2 renders after a mutation!!!

from react-apollo-hooks.

juicylevel avatar juicylevel commented on August 16, 2024 1

Is this problem solved somehow?

from react-apollo-hooks.

fbartho avatar fbartho commented on August 16, 2024

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.

frangolet avatar frangolet commented on August 16, 2024

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.

mbrowne avatar mbrowne commented on August 16, 2024

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.

aadamsx avatar aadamsx commented on August 16, 2024

Any word on this?

from react-apollo-hooks.

yantakus avatar yantakus commented on August 16, 2024

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.

yantakus avatar yantakus commented on August 16, 2024

@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.

JClackett avatar JClackett commented on August 16, 2024

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.

JClackett avatar JClackett commented on August 16, 2024

use @apollo/react-hooks :)

from react-apollo-hooks.

twelve17 avatar twelve17 commented on August 16, 2024

@JClackett Are you no longer having the other issues that prevented you from using @apollo/react-hooks?

from react-apollo-hooks.

JClackett avatar JClackett commented on August 16, 2024

@twelve17 @apollo/react-hooks works for me now, so have been using that in all my projects

from react-apollo-hooks.

SyedAsimAliSE avatar SyedAsimAliSE commented on August 16, 2024

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.

sujed avatar sujed commented on August 16, 2024

For me also was the problem React.StrictMode

from react-apollo-hooks.

torontochen avatar torontochen commented on August 16, 2024

same here

from react-apollo-hooks.

clovisdasilvaneto avatar clovisdasilvaneto commented on August 16, 2024

Did someone found a way to solve this problem? I'm also facing this issue.

from react-apollo-hooks.

ppsirius avatar ppsirius commented on August 16, 2024

Remove Strict Mode Hoc but is just a workaround ;)

from react-apollo-hooks.

pgriscti avatar pgriscti commented on August 16, 2024

useQuery(yourQuery, { fetchPolicy: 'no-cache' })

from react-apollo-hooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.