admin管理员组文章数量:1430551
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
orJSON.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
andReact.memo
does shallow paring. So re-render doesn't happen if the nested data changes. – Raviteja Commented May 8, 2019 at 12:18
1 Answer
Reset to default 21 - 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
版权声明:本文标题:javascript - Deep comparing object props in React not working as expected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745479822a2660126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论