【发布时间】:2016-08-18 05:38:05
【问题描述】:
我对 React 和 React Native 还很陌生,并且在使用它时我会努力牢记性能。
我在某处读到,将console.log(...) 放入我的组件的render(...) 方法中是个好主意,这样我就可以看到它们被渲染的频率。
我在用户看到的第一个屏幕上做了这个,我注意到它立即渲染了 3 到 4 次。
这个方法的代码定义如下,只渲染3个部分。
下面的代码中是否有任何内容不正确或以非性能方式完成,我应该采取不同的做法?例如,我在几个地方读到我绑定回调的方式不正确,并且会注册多次(每次渲染完成)。
另外,render(...) 多次执行是否正常或正常,还是可以避免?
class Home extends Component {
_onRequestItemClick(id){
alert(id);
}
_onMakeRequestClick(){
this.props.dispatch(navigatePush('Create Request'));
}
render() {
console.log('Rendering Home...');
return (
<View style={styles.container}>
<View style={[styles.base,styles.halfHeight]}>
{this._renderRequestList()}
</View>
<View style={[styles.base,styles.quarterHeight]}>
{this._renderMakeRequestButton()}
</View>
<View style={[styles.quarterHeight]}>
{this._renderFulfillmentScroller()}
</View>
</View>
);
}
_renderRequestList(){
let { requests } = this.props;
return (
<RequestList
requests={requests}
onRequestItemClick={this._onRequestItemClick.bind(this)}
/>
);
}
_renderMakeRequestButton(){
return (
<ActionButton
title="Make Request"
onActionPress={this._onMakeRequestClick.bind(this)}
/>
);
}
_renderFulfillmentScroller(){
let { fulfillments } = this.props;
var onPressItem = (item)=> alert('item selected:' + item.id);
return (
<CalendarBlockScrollView
bounces={false}
style={styles.scroller}
itemStyle={styles.fulfillment}
items={fulfillments}
onPressItem={onPressItem}
/>
);
}
}
function mapDispatchToProps(dispatch) {
return {
dispatch
};
}
function mapStateToProps(state) {
return {
userId:state.get('profile').current.id,
requests:state.get('requests').all.requests,
fulfillments: state.get('fulfillments').all.fulfillments
}
}
export default connect(mapStateToProps,mapDispatchToProps)(Home);
【问题讨论】:
-
shouldComponentUpdate
标签: reactjs react-native redux react-redux