在NavigatorIOS中有一個2層深的按鈕,當觸發時,應該改變TabBarIOS選中的TabBarIOS項。孫子組件不改變祖父母狀態
的組件的結構如下:
--FooterTabs
------ NavigatorIOS:列表 - >列表項
我試圖通過具有內部FooterTabs一個函數來做到上述改變所述狀態,並且當狀態的'selectedTab'===不同的字符串時,TabBar.Items的prop'selected'變爲true。
像這樣:
_changeToAlarms(){
console.log('Hello from Footer Tabs');
this.setState({
selectedTab: 'Alarms'
});
}
render(){
return(
<Icon.TabBarItemIOS title={'Alarms'}
iconName='ios-clock'
selected={this.state.selectedTab === 'Alarms'}
onPress={()=> {
this.setState({
selectedTab: 'Alarms'
});
}}>
<ComingSoon/>
</Icon.TabBarItemIOS>
<Icon.TabBarItemIOS title={'Schedules'}
iconName='ios-moon'
selected={this.state.selectedTab === 'Schedules'}
onPress={()=> {
this.setState({
selectedTab: 'Schedules'
});
}}>
</Icon.TabBarItemIOS>
);
}
* Icon.TabBatItem行爲酷似TabBarIOS.Item。
onPress工作,但能夠以類似的方式(通過修改組件的狀態)改變選項卡我通過_changeToAlarms()作爲'SleepSchedules'的道具。
<Icon.TabBarItemIOS ...
>
<NavigatorIOS
initialRoute={{
component: SleepSchedules ,
title: ' ',
passProps: {changeToAlarms:()=> this._changeToAlarms()}
}}
style={{flex: 1}}
/>
</Icon.TabBarItemIOS>
而且從SleepSchedules,我導航到下一個組件並傳遞previous'changeToAlarms'作爲道具。
_handlePress(selectedSchedule){
this.setState({selectedSchedule});
this._navScheduleItem(selectedSchedule)
}
_navScheduleItem(scheduleName){
this.props.navigator.push({
title: `${scheduleName} Sleep`,
component: ScheduleItem,
passProps: {scheduleName}, changeToAlarms:()=> this.props.changeToAlarms
})
}
render(){
return(
...
onPress={()=> this._handlePress('Monophasic')}>
);
}
而且在ScheduleItem我試圖打電話給道具「changeToAlarm」已傳遞,這應該是從頁腳標籤之一。
_handleTouch(){
console.log('Hello from Schedule Item');
this.props.changeToAlarms;
}
render(){
return(
...
onPress={()=> this._handleTouch()}
);
}
控制檯登錄「你好從ScheduleItem」每次我按下它的時候,但不記錄「從FooterTabs你好」,也沒有改變的標籤。
有沒有人發現錯誤?
我是初學者,非常感謝您的幫助! :)
我不知道這是否是解決問題的正確方法,但這是我對它的概念。如果我的方法有任何錯誤,請隨時評論正確的方法。 –