admin管理员组

文章数量:1426617

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: '

本文标签: javascriptDeep comparing object props in React not working as expectedStack Overflow