私は出力のためにオブジェクトの配列をプレゼンテーションコンポーネントに渡すコンテナコンポーネントを持っています。React:単純なロジックをContainerまたはPresentationコンポーネントに入れるには?
プレゼンテーションコンポーネントでは、特定の基準を満たすこれらのオブジェクトの数を表示する必要があります。コンテナコンポーネントでカウントを実行し、それをプレゼンテーションコンポーネントに渡すのがベストプラクティスか、プレゼンテーションコンポーネントでこのカウントを実行することは問題ありません。
すなわち:
export class ResultsPage extends React.Component {
constructor(props){
super(props);
}
countSexyObjects(){
const matching = this.props.allObjects.filter((obj)=>{
return obj.sexy === true;
});
return matching.length
}
render(){
return (
<PresentationalComponent allObjects={this.props.allObjects}
numberOfSexyObjects={this.countSexyObjects()} />
);
}
}
let PresentationalComponent = (props) => {
return (
<div>
There are {props.numberOfSexyObjects} sexy objects
</div>
);
};
OR
export class ResultsPage extends React.Component {
constructor(props){
super(props);
}
render(){
return (
<PresentationalComponent allObjects={this.props.allObjects} />
);
}
}
let PresentationalComponent = (props) => {
const countSexyObjects =() => {
const matching = this.props.allObjects.filter((obj)=>{
return obj.sexy === true;
});
return matching.length
};
return (
<div>
There are {countSexyObjects()} sexy objects
</div>
);
};
このリンクも参考になります - https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0#.2vdpxfulp – kaushik94