question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Objects within list change after mutation of the list

See original GitHub issue

Hi there,

I did not find a related discussion, so I’ve opened a new issue. Not sure though if it is an issue or intended behaviour (just not mine). Thanks in advance for shutting some light on this and any kind of explanation!

I’ve been trying Apollo on a simple list to test paging and simple mutations. For simplicity assume the following easy query-result structure…

[
 { id: "item-1", value: "a" },
 { id: "item-2", value: "b" },
 { id: "item-3", value: "c" },
...
]

This result is passed to a List component and each entry is then passed to ListItem components. So far so good.

I then delete “item-2” via a mutation.

const id = 'item-2';
mutate({
        variables: { id },
        resultBehaviors: [
          {
            type: 'DELETE',
            dataId: id,
          },
        ],
      }),

That’s pretty neat, as the item is automatically removed from the list and of course the list re-renders due to the changed list. But all my remaining list items are re-rendered as well!

Although all other objects within the list have not been touched by the mutation, they get re-created somehow (new reference, same values) and a simple === check in shouldComponentUpdate of my list item does not help anymore…

The same goes for paging. When incorporating fetchMore results into the list, all the previous items of the list seem to be re-created as well. And thus re-render. I am implementing paging to reduce the required network-traffic of course, but also to reduce the amount of items I need to render in one go (10, then 10 more, etc), but now it renders 10, then 20, 30, … which is not what I want to achieve.

When implementing such things in previous projects, I used immutable.js for my stores, especially due to performance concerns. I went with the assumption that the Apollo Client would do these things under the hood (out of the box). Or at least the remaining items should be left untouched.

I am pretty confused about this behaviour. Maybe I am missing a simple point and did a mistake. Any explanation on that matter would be highly appreciated!

Thanks and kind regards, Daniel

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
kachkaevcommented, Nov 25, 2016

We’re also facing this issue (see #945) and it causes pretty large chunks of virtual DOM to be re-rendered. Let’s say you have three lists with 1000 items in each and you only mutate one attribute in one item. Even if you carefully implement updateQueries and patch only one little node out of the whole data tree, all related components fail to shallow compare their props.

We’d be really glad to see this fixed in Apollo client!

1reaction
calebmercommented, Jan 19, 2017

Referential equality for data returned by the Apollo Client was implemented in https://github.com/apollostack/apollo-client/pull/1136 and released in 0.7.x so now to see if data has changed you can simply use === 👍

Let me know if this isn’t enough.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python list mutation after copying - Stack Overflow
In mutation , input_list starts out pointing at the same object as ... The simplest solution is changing value of first element of...
Read more >
Lists, mutability, and in-place methods
Lists are ordered collections of other objects. And lists are mutable.
Read more >
LIST MUTATION, ORDERS OF GROWTH, AND NONLOCAL
We can change the elements of list objects after we've created it. If I assign this variable a to variable b, b receives...
Read more >
The 2 types of "change" in Python
The second type of "change" is mutation. Assignment changes variables, but mutation changes objects. Most Python objects can be changed after ...
Read more >
Programming FAQ — Python 3.11.1 documentation
Contents: Programming FAQ- General Questions- Is there a source code level debugger ... What does the slash(/) in the parameter list of a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found