Javascript 如何使用react shouldComponentUpdate()生命周期方法比较对象的嵌套数组
我正在寻找一种最佳方法,在使用shouldComponentUpdate时,我可以将状态/道具中的当前值与状态/道具中的新值进行比较Javascript 如何使用react shouldComponentUpdate()生命周期方法比较对象的嵌套数组,javascript,reactjs,react-dom,Javascript,Reactjs,React Dom,我正在寻找一种最佳方法,在使用shouldComponentUpdate时,我可以将状态/道具中的当前值与状态/道具中的新值进行比较 shouldComponentUpdate(nextProps, nextState) 让我考虑一个例子,如果在我的状态中有以下数据 [ { title: "Hello", id: 1, isUpdated: false, property: [
shouldComponentUpdate(nextProps, nextState)
让我考虑一个例子,如果在我的状态
中有以下数据[
{
title: "Hello",
id: 1,
isUpdated: false,
property: [
{
name: "property 1",
area: "area 1",
inspection: [
{
isNew: true,
isSold: false,
isLicenseExpired: false
}
]
},
{
name: "property 2",
area: "area 2",
inspection: [
{
isNew: false,
isSold: false,
isLicenseExpired: true
}
]
}
]
},
{
title: "World",
id: 2,
isUpdated: false,
property: [
{
name: "property 1",
area: "area 1",
inspection: [
{
isNew: false,
isSold: false,
isLicenseExpired: true
}
]
},
{
name: "property 2",
area: "area 2",
inspection: [
{
isNew: false,
isSold: false,
isLicenseExpired: true
}
]
}
]
}
]
现在,当我用相同的数据再次更新状态时,我无法手动将其与上一个和当前的道具/状态进行比较,因为如果我像
nextrops.data!==这个.props.data
会给出错误的结果,因为它只进行了肤浅的比较
我还知道我可以使用lodash库来深入比较值,但我想知道比较shouldComponentUpdate()
下面是一个实例,以防您有兴趣看到我的代码和问题
谢谢您的时间。对于您的问题,这篇文章的答案可能太多了。stringify是一种方法,但速度很慢。Lodash,下划线也起作用。或者您可以使用线程中响应程序提供的函数之一