我有一个 React.js 组件,它的呈现方式如下:
<Social email={this.state.email} />;
页面上有一些事件是 update this.state.email
,结果是通过 render ,它会向组件发送一个新的email
prop Social
。
在这个Social
组件中,我正在监听这样的更新:
componentWillReceiveProps: function(nextProps) {
console.log('received props update', nextProps.email);
this.doSomeWork();
}
该控制台行被渲染两次,这使得 UI 随着对社交网络的调用而闪烁两次。
我总是可以做这样的事情:
if (nextProps.email != this.props.email) {
this.doSomeWork();
}
但是感觉有点坑...
是预期的双重消息吗?如果是这样,好奇为什么?
如果没有,追踪和消除它的最佳方法是什么?