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.

Get over shallow comparison issues?

See original GitHub issue

I was working on an issue today that I wasn’t able to explain in other way than shallow comparison.

Essentially I have a state similar to following:

{
 meta: {},
 contents: [
   { content_type: 1, text:"" },
   { content_type: 2, text:"" },
   { content_type: 1, text:"" }
 ]
}

there are many content types, this is just a simple example. What I am doing is re ordering these in state to cause re ordering on ui side, ie I can move first object from contents array down or second one up just fine, but when it comes to re ordering 2 object with same content type, it successfully updates the redux state, however react doesn’t re-render the ui, and I came to conclusion that this could be caused by shallow comparison, as objects that switched places are very very similar, only their text fields are different.

I am looking for a way to work around this.

following is how I update my redux state:

...
case STORY_ACTIONS.UPDATE_STORY_CONTENTS:
      return { ...state, contents: action.payload }
...

where action.payload is correctly re-ordered contents array, I don’t think this is an issue, as it does update redux state correctly.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
0xAsimetriqcommented, Feb 24, 2016

@Sunakujira1 I got it! so I was setting key of each storyBlock to index gathered from map() somehow this led to isues, changing this key to something like uniqe random id I generated for each block helps.

2reactions
gaearoncommented, Feb 25, 2016

This is why I asked about the local state. If you don’t specify the key, components will receive each other’s props on reorder. Which makes reorder slower but that is not the root of the problem.

The root of the problem is you derive local state from initial props in the constructor. But props may be received by the component more than once. When your components receive new props during reorder, you forget to reset the state to reflect those new values.

To do it, you would need to implement componentWillReceiveProps(nextProps) that calls setState if nextProps.text !== this.props.text. Adding a key also helps but it heals the symptom rather than the actual cause of the problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shallow Comparison vs Deep Comparison in Javascript
You most likely heard terms shallow comparison and deep comparison. In this post you will fully understand how it all works. In this...
Read more >
Shallow Comparison vs Deep Comparison in Javascript
Learn the difference between shallow comparison, deep comparison and javascript comparison by building this functions with me from scratch.
Read more >
How Does Shallow Comparison Work In React? - Chak Shun Yu
Shallow comparison is a concept all over React development and documentation. It plays a key role in React's internals, but it's not often ......
Read more >
Optimizing React.js Performance with PureComponents and ...
js Performance with PureComponents and Shallow Comparison. Today, we will be going over how to use React.PureComponent to speed up ...
Read more >
Learn About React Pure Components, Shallow Comparison ...
Shallow comparison works by comparing primitive types by their value, and data structure types / reference types by comparing if they reference ...
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