componentWillReceiveProps(nextProps) {
console.log('this.props is:', this.props)
console.log('nextProps is:', nextProps)
let categoryIdChange = this.props.categoryId !== nextProps.categoryId,
parentCategoryIdChange = this.props.parentCategoryId !== nextProps.parentCategoryId
// 数据没有发生变化的时候,直接不做处理
if (!categoryIdChange && !parentCategoryIdChange) {
console.log('数据不做处理')
return
}
// 假如只有一级品类
if (nextProps.parentCategoryId === 0) {
this.setState({
firstCategoryId: nextProps.categoryId,
secondCategoryId: 0
})
}
// 有两级品类
else {
this.setState({
firstCategoryId: nextProps.parentCategoryId,
secondCategoryId: nextProps.categoryId
},
() => {
parentCategoryIdChange && this.loadSecondCategory()
}
)
}
}运行场景是: 某商品有一级和二级品类,当第一次点编辑按钮进入到该商品编辑页面的时候 通过console.log打印出如下信息

发现"数据没有发生变化的时候,直接不做处理"这段代码运行了。我的问题是为什么调用了loadSecondCategory后 componentWillReceiveProps 又被执行了一次? 这种行为对吗? 而我的环境按照老师的代码写下来 同样的场景 在loadSecondCategory调用后 却不会第二次调用componentWillReceiveProps。
麻烦老师解答下,谢谢!