我有一些组件应该在它们的数据首次到达并呈现后立即执行一些工作,但不用于将来的重新呈现。例如:注释已加载并呈现,现在1.加载社交媒体库和2.加载一些Google Analytics(分析)。
现在我正在这样做:
componentDidUpdate: function (prevProps, prevState) {
if (this.hasAlreadyUpdatedOnce) {
// ... do some stuff...
} else {
// ... do some stuff that should happen only once...
// 1. load social media libraries
// 2. load some Google Analytics
this.hasAlreadyUpdatedOnce = true;
}
}
但是我问自己,是否有比设置类似属性更优雅的方法。
假设您正在响应状态更改,则应将回调作为第二个参数传递给setState。
componentDidMount: function(){
ajaxyThing(function(data){
this.setState({data: data}, function(){
// this.state is updated, the component has rerendered
// and the dom is current
});
}.bind(this));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句