javascript - Deep comparing object props in React not working as expected - Stack Overflow

I am optimizing performance of a React app by reducing unnecessary re-renders. The ponent I am working

I am optimizing performance of a React app by reducing unnecessary re-renders. The ponent I am working on receives a single prop containing an object with many keys including array of objects. I am using shouldComponentUpdate to check for changes in prop, but it's not working as expected:

shouldComponentUpdate(nextProps) {
    if (!(_.isEqual(nextProps, this.props))) {
        console.log('difference', _.differenceWith(nextProps, this.props));
        return true;
    }

  return false;
}

isEqual is a method from Lodash for deep paring objects. differenceWith is a Lodash method for finding difference in two objects. The weird thing is that using isEqual does not reduce re-renderings, and differenceWith prints an empty array []. But if I use JSON.stringify instead of isEqual, re-renders are reduced by half, but differenceWith still prints [].

shouldComponentUpdate(nextProps) {
    if ( JSON.stringify(nextProps) !== JSON.stringify(this.props) ) {
        console.log('difference', _.differenceWith(nextProps, this.props));
        return true;
    }

  return false;
}

Why is there a difference in behaviour of isEqual and JSON.stringify when they are essentially doing the same thing, although in a different way (note that isEqual is also order sensitive)?

What is the best way to avoid re-renderings here?

I am optimizing performance of a React app by reducing unnecessary re-renders. The ponent I am working on receives a single prop containing an object with many keys including array of objects. I am using shouldComponentUpdate to check for changes in prop, but it's not working as expected:

shouldComponentUpdate(nextProps) {
    if (!(_.isEqual(nextProps, this.props))) {
        console.log('difference', _.differenceWith(nextProps, this.props));
        return true;
    }

  return false;
}

isEqual is a method from Lodash for deep paring objects. differenceWith is a Lodash method for finding difference in two objects. The weird thing is that using isEqual does not reduce re-renderings, and differenceWith prints an empty array []. But if I use JSON.stringify instead of isEqual, re-renders are reduced by half, but differenceWith still prints [].

shouldComponentUpdate(nextProps) {
    if ( JSON.stringify(nextProps) !== JSON.stringify(this.props) ) {
        console.log('difference', _.differenceWith(nextProps, this.props));
        return true;
    }

  return false;
}

Why is there a difference in behaviour of isEqual and JSON.stringify when they are essentially doing the same thing, although in a different way (note that isEqual is also order sensitive)?

What is the best way to avoid re-renderings here?

Share Improve this question edited May 8, 2019 at 13:36 darKnight asked May 8, 2019 at 11:11 darKnightdarKnight 6,49115 gold badges58 silver badges93 bronze badges 3
  • If your Object is nested you need to either use lodash deepClone or JSON.parse(JSON.strinfiy(Obj)) – Michael Commented May 8, 2019 at 11:14
  • I don't see how this is going to help? deepClone is for cloning objects which is not required here and there is no sense in converting an object to json and then back to object..! – darKnight Commented May 8, 2019 at 11:32
  • shouldComponentUpdate,PureComponent and React.memo does shallow paring. So re-render doesn't happen if the nested data changes. – Raviteja Commented May 8, 2019 at 12:18
Add a ment  | 

1 Answer 1

Reset to default 2

1 - Why is there a difference in behaviour of isEqual and JSON.stringify when they are essentially doing the same thing, although in a different way (note that isEqual is also order sensitive)?

Take a look on what I found here.

Well that depends. For JSON.stringify(), the order matters. So if the key-value pair are ordered differently in the two objects but are the same, it will return false. Whereas it doesn't matter in Lodash isEqual, it will return true as along as the key-value pair exists.

const one = {
  fruit: '

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745479823a4629510.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信