我有一个TABNAVIGATOR组件,我希望单个TAB屏幕屏幕状态可以在TAB按下重置。如何在选项卡上调用类(组件)方法?我已经查看了Tabbaronpress,但这从父级(TABNAVIGATOR)获取了一个函数。我希望触发类方法,并且我不想导出该方法。
假设您的TabNavigator这样
const HOME = TabNavigator({
[SCREENS.EXPLORESCREEN]: { screen: ExploreScreen },
[SCREENS.ASKSCREEN]: { screen: AskScreen },
},{
initialRouteName: SCREENS.EXPLORESCREEN
})
const PrimaryNav = StackNavigator({
[SCREENS.HOMESCREEN]: {
screen: HOME,
navigationOptions=({navigation})=>{
return{
tabBarOnPress :()=>{
if (typeof navigation.state.params.cancelState!=="undefined"){
navigation.state.params.cancelState()
}
else{
//your stuff
}
}
}
}
},
},
{
// Default config for all screens
headerMode: "screen",
initialRouteName: SCREENS.HOMESCREEN
,
}
);
探索
class ExploreScreen extends Component {
componentDidMount(){
this.props.navigation.setParams({
cancelState:this.cancelState
})
}
cancelState=()=>{
//your Stuff
}
}
- 您也可以在组件级别使用导航。