React-apollo: Useless re-render component with `graphql(MYQUERY)(Navbar)` everytime.

Created on 18 Apr 2018  路  7Comments  路  Source: apollographql/react-apollo

Intended outcome:

Do not re-render simply component in App with graphql(MYQUERY)(Navbar) everytime when I change route because I already have query results in cache.

Actual outcome:

It re-renders my Navbar component useless. It doesn't need re-render because graphql(MYQUERY) already in cache.

How to reproduce the issue:

Simply go here: https://codesandbox.io/s/zqk59nkmk4 and click "Home" and "Players". In console you will see "Navbar this.props e nextProps different" every time you change route.

image

Version

@jbaxleyiii, maybe the same as: https://github.com/apollographql/react-apollo/issues/1777 ?

@tnrich, maybe the same as: https://github.com/apollographql/react-apollo/issues/1870 ?

has-reproduction

Most helpful comment

Having the same Issue, Query rerenders when changing route in React Router

All 7 comments

Here there is the same problem with Query component and without graphql HOC.

I believe this is react-router rerending your Navbar.
You can try for yourself by removing all graphql related code in your example.

Having the same Issue, Query rerenders when changing route in React Router

Hi guys, is there any plan to fix this issue?

Same here. I'am afraid rerender happening because graphql HOC produce each time new instance of query object into props even if data are same.

My workaround with recompose:
```
...
graphql(MYQUERY)
branch(({ data }) => data.loading, renderComponent(LoadingComponent)),
lifecycle({
shouldComponentUpdate(nextProps) {
//compare query response data from current and next props
}
})

@adorum I was thinking to create a variable outside the graphql() HOC, and provide the same references of first level objects of data props in graphql() second arguments (the config object) props property. But this is just an overhead - it would be better to solve it inside graphql().
example:

const propsData = {};

graphql(MY_QUERY, {
  props: (data) => {
    // do the magic with propsData 
  }
})

This should no longer be an issue with current day versions of react-apollo, but if anyone is still encountering this issue (and can provide a small runnable reproduction), we'll take a look. Thanks!

Was this page helpful?
0 / 5 - 0 ratings