私はReact and React Nativeの新機能を使用しています。Reactネイティブレンダリングメソッドの呼び出し3-5x
console.log(...)
を私のコンポーネントのメソッドに入れて、レンダリングの頻度を確認できるようにしました。
これは私の最初のスクリーンの1つで、私はこれを3〜4回レンダリングしていることに気付きました。
このメソッドのコードは以下に定義されており、3つのセクションをレンダリングするだけです。
以下のコードに、間違って実行されているか、または私が別のやり方で実行すべきでない非実行的な方法がありますか?たとえば、私はコールバックをバインドする方法が正しくなく、複数回(レンダリングが完了するたびに)登録されるいくつかの場所を読みます。
また、を複数回実行するのは問題ないですが、回避することはできますか?
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 – Sulthan
まず(https://daveceddia.com/avoid-bind-when-passing-props/ [小道具にバインドを使用しない方法]読み) – stereodenis