我建议跟踪活动选项卡并管理DisplayController组件中的所有API调用。使用下面的结构应该足以让你开始。数据获取函数存在于Display组件中,并通过道具中的回调传递给其他组件。
var DisplayController = React.createClass({
getInitialState: function(){
return {
active_tab: 0, display_data: [],
tabs: [{name: 'Tab 0', tab_id: 0}, {name: 'Tab 1', tab_id: 1}, {name: 'Tab 2', tab_id: 2}]
};
}
changeTab: function(tab_id){
this.setState({active_tab: tab_id}, apiCall);
}
apiCall: function(){
//make api call based off of this.state.active_tab
//this.setState({display_data: whatever you get back from api})
}
render: function(){
dprops = {
tabs: this.state.tabs.
changeTab: this.changeTab,
active_tab: this.state.active_tab,
display_data: this.state.display_data
};
return (<MainMenu {...dprops}/>);
}
});
var MainMenu = React.createClass({
changeTab: function(tab_id){
this.props.changeTab(tab_id);
},
render: function(){
tabs = this.props.tabs.map(function(tab){
return <Tab onClick={this.changeTab.bind(tab.tab_id)} name={tab.name} key={tab.tab_id}/>
}.bind(this));
return(
<div>
{tabs}
<Display {...this.props} />
</div>
);
}
});
var Tab = React.createClass({
render: function(){
// make your tab component here from this.props.name
}
});
var Display = React.createClass({
render: function(){
// make your display component here using data from this.props
}
});
我想你可以创建一个JavaScript自定义事件来触发标签变化,并在显示组件,该事件 –
反应你能提供一个例子添加监听? – ApathyBear
请查看https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events –