Reactjs 道具与componentDidUpdate不正确配合。(现金污染)

Reactjs 道具与componentDidUpdate不正确配合。(现金污染),reactjs,Reactjs,我有一个道具,每次该道具更改时都需要更新值。值来自API,当道具更改时更新正常,但随后,我看到在“网络”部分持续记录此服务,因为componentdiddupdate()updates!不幸的是,这会造成污染 发送道具: 接收道具: getPoints=()=>{ 让公司=this.props.company.value; get(“/api/point”,{params:{company}}) .然后(res=>res.data) 。然后(res=>{ const points=res.m

我有一个道具,每次该道具更改时都需要更新
值。值来自API,当道具更改时更新正常,但随后,我看到在“网络”部分持续记录此服务,因为
componentdiddupdate()
updates!不幸的是,这会造成污染

发送道具:


接收道具:

getPoints=()=>{
让公司=this.props.company.value;
get(“/api/point”,{params:{company}})
.然后(res=>res.data)
。然后(res=>{
const points=res.map(点=>{
返回{
标签:point.name,
值:point.id
};
});
this.setState({points});
})
.catch(错误=>{
控制台日志(err);
});
};
componentDidUpdate()

componentDidUpdate(){
这是getPoints();
}
内部渲染():


销售点
“从不同的列表中找到您的观点”}
选项={points}
占位符=“选择点…”
/>

getPoints
调用的函数正在更新状态,它依次调用
componentdiddupdate
getPoints
等等

componentdiddupdate
接收一个参数
prevProps
,该参数可用于检查
props
是否已从上次更新更改

componentDidUpdate(prevProps) {
  // you would need to safeguard the condition where company does not exist.
  if(prevProps.company.value !== this.props.company.value) {
    this.getPoints();
  }
}



getPoints
调用的函数正在更新状态,它依次调用
componentdiddupdate
getPoints
等等

componentdiddupdate
接收一个参数
prevProps
,该参数可用于检查
props
是否已从上次更新更改

componentDidUpdate(prevProps) {
  // you would need to safeguard the condition where company does not exist.
  if(prevProps.company.value !== this.props.company.value) {
    this.getPoints();
  }
}



除此之外,您还可以使用PureComponent,它会自动处理您遇到的问题

此外,还可以实现shouldComponentUpdate()生命周期方法来决定是否调用render()


希望这也有帮助

除此之外,您还可以使用PureComponent,它会自动处理您遇到的问题

此外,还可以实现shouldComponentUpdate()生命周期方法来决定是否调用render()


希望这也有帮助

谢谢你,银色翅膀的男孩,很管用。也许我需要更好地理解这些生命周期是如何运作的。顺便说一句,我可能找到了其他解决方案:
//UNSAFE_组件将接收props(newProps){//this.getPoints(newProps);//}
,但更愿意使用您的。谢谢!谢谢你,银色翅膀的男孩,很管用。也许我需要更好地理解这些生命周期是如何运作的。顺便说一句,我可能找到了其他解决方案:
//UNSAFE_组件将接收props(newProps){//this.getPoints(newProps);//}
,但更愿意使用您的。谢谢!谢谢,看来我在未来的“快跑”中需要这些知识。我听说过PureComponent,但是shouldComponentUpdate()对我来说是新的。您可以在这里阅读更多关于shouldComponentUpdate()的内容,如果它解决了您的问题,请将答案标记为已接受,以便它可以帮助其他人。谢谢:)谢谢,看来我在未来的“快跑”中需要这些知识。我听说过PureComponent,但是shouldComponentUpdate()对我来说是新的。您可以在这里阅读更多关于shouldComponentUpdate()的内容,如果它解决了您的问题,请将答案标记为已接受,以便它可以帮助其他人。谢谢:)