我有一个TabNavigator组件,我希望单个选项卡屏幕状态在选项卡上重置。如何在选项卡上调用类(组件)方法?我查看了tabBarOnPress,但是它从父级别(TabNavigator)获取了一个函数。我希望触发类方法,我不想导出该方法。
答案 0 :(得分:3)
假设你有像这样的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
,
}
);
ExploreScreen.js
class ExploreScreen extends Component {
componentDidMount(){
this.props.navigation.setParams({
cancelState:this.cancelState
})
}
cancelState=()=>{
//your Stuff
}
}