2
我正在调用一个API,它允许您按不同参数(例如top,latest,popular等)返回的数据进行排序。我试图实现的是,当用户单击一个按钮按不同的参数进行排序时,状态将更改为新参数,并使用该新参数再次调用API。这里是我的代码:React/Flux seState在API调用后不重绘渲染组件
constructor(){
super();
this.state = {
sortType: 'top' //default is to sort by Top
};
this.setSortType = this.setSortType.bind(this);
}
componentWillMount(){
//this retrieves sourceName which is required to make the API call
const parsed = queryString.parse(this.props.location.search);
var sourceName = parsed.sourceId;
//getArticles below is the action that makes the API call with sourcename and sortType as parameters
newsActions.getArticles(sourceName,this.state.sortType);
newsStore.on('articles_change',this.fetchNewsArticles);
}
//Call to setState when user clicks button change sort parameter
setSortType(){
this.setState({
sortType:'latest'
});
}
render() {
return (
<div>
... //logic to display data from the API call
<button onClick={this.setSortType}> Sort By Latest </button>
</div>
);
}
当按钮被点击时,什么都不重新呈现。我错过了什么?