親コンポーネントからプロパティを取得し、このプロパティに基づいてAJAXリクエストを行うコンポーネントが必要です。親コンポーネントはこのプロパティを変更でき、子コンポーネントは別の1つのAJAXリクエストを取得する必要があります。ここで プロパティが変更されたときにAJAX要求を作成します
は私のコードですが、私はそれが最適とさえ正確であることを確認していない:<News source={this.state.currentSource} />
コンポーネント:
var News = React.createClass({
propTypes: {
source: React.PropTypes.string
},
getInitialState: function() {
return {
entities: []
};
},
componentWillReceiveProps(nextProps) {
var url = 'http://localhost:3000/api/sources/' + nextProps.source + '/news';
this.serverRequest = $.get(url, function(result) {
this.setState({
entities: result
});
}.bind(this));
},
componentWillUnmount: function() {
this.serverRequest.abort();
},
render: function() {
// ...
}});
module.exports = News;
componentWillReceiveProps
は、このコードのために良い場所ですしていますか?