Reactjs 组件将接收Props vs getDerivedStateFromProps
不推荐使用ComponentWillReceiveProps,提倡使用getDerivedStateFromProps 但是当道具发生更改时,ComponentReceiveProps将被调用,而getDerivedStateFromProps将在任何状态更改时被调用 我的要求是重置道具更改时的状态。那我怎么知道道具是否真的换了呢 我不赞成将props值存储在state中,因为它会不必要地使state变得更庞大,而且我还没有考虑到state管理(Redux或contextapi) 请告知 我怎么知道道具是否真的换了 将以前的道具与新道具进行比较 对于最简单的用例,只需使用,并进行比较:Reactjs 组件将接收Props vs getDerivedStateFromProps,reactjs,Reactjs,不推荐使用ComponentWillReceiveProps,提倡使用getDerivedStateFromProps 但是当道具发生更改时,ComponentReceiveProps将被调用,而getDerivedStateFromProps将在任何状态更改时被调用 我的要求是重置道具更改时的状态。那我怎么知道道具是否真的换了呢 我不赞成将props值存储在state中,因为它会不必要地使state变得更庞大,而且我还没有考虑到state管理(Redux或contextapi) 请告知 我怎么
componentDidUpdate(prevProps) {
// Typical usage (don't forget to compare props):
if (this.props.userID !== prevProps.userID) {
this.fetchData(this.props.userID);
}
}
根据,可以执行副作用或setState()
内部componentdiddupdate()
而setState()
会导致重新渲染,从而影响组件性能。当这种情况下的性能实际上是一件事情时,请继续阅读: