Open a modal on BottomTabNavigator click react-native

最后都变了- 提交于 2019-12-24 10:19:08

问题


I was trying to open a modal from bottomnavigator , was following this tutorial - https://snack.expo.io/SyJKMkFUM I am using react-navigation -3.0.9

here is my app.js class App extends React.Component {

 renderItem = (route, index) => {
     const {
         navigation,
         jumpToIndex,
     } = this.props;
     const isCapture = route.routeName === 'Capture';
     const focused = index === navigation.state.index;
     return (
         <TouchableWithoutFeedback
             key={route.key}
             onPress={() => isCapture ? this.props.navigation.navigate('CaptureModal') : jumpToIndex(index)}
         >
             <View >
                 <Text >{route.routeName}</Text>
             </View>
         </TouchableWithoutFeedback>
     );
 };
 render() {
     const {
         navigation,
     } = this.props;

     const {
         routes,} = navigation.state;
       return (
         <View style={styles.container}>
             {routes && routes.map(this.renderItem)}
         </View>
     );}}
   const Screen = (props) => (
    <View >
    <Text>{props.title} Screen</Text>
    </View>
    );
    const styles = StyleSheet.create({
       container: {
       flex: 1,
        backgroundColor: '#fff',
       },
        });
      const TabNavigator = createBottomTabNavigator({
      Home: {
    screen: HomeScreen
},
Link: {
    screen: LinksScreen
},
settings: {
    screen: View,
},
 });
   const CaptureStack = createStackNavigator({
   Capture: {
    screen: (props) => <Screen title="Capture" {...props} />,
    navigationOptions: ({ navigation }) => ({
        headerTitle: 'Capture',
        headerLeft: (
            <Button
                title="Back"
                onPress={() => navigation.goBack(null)}
            />),}),},})

      const RootStack1 = createStackNavigator({
     Main: {
    screen: TabNavigator,
       },
        CaptureModal: {
    screen: CaptureStack,
    navigationOptions: {
        gesturesEnabled: false,
        },
      },
     }, {
headerMode: 'none',
mode: 'modal',
   });

const AppNavigator = createAppContainer(RootStack1); export default AppNavigator;

Could anyone please explain what is wrong with this ? Could it be a routing version issue as the tutorial is using 1.0.0 .


回答1:


I did solve my issue if anyone is lokking here how i achieved it -

My Goal ->
Click on Main Screen -> Tabbed Activity -> on click of any tab new screen

in my app.js i created navigation as follows -

const TabNavigator = createBottomTabNavigator({
    HomeScreen: {
    screen: HomeScreen
},
    LinksScreen: {
    screen: LinksScreen
},
    LoginScreen: {
    screen: SurveyScreen,
},

 },
{
 initialRouteName : 'HomeScreen' ,
});

const RootStack1 = createStackNavigator({
TabNavigator: {
    screen: TabNavigator,
},
CaptureModal: {
    screen: LoginScreen,
     navigationOptions: {
         gesturesEnabled: false,
     },
  },
}, {
headerMode: 'none',
mode: 'modal',
headerLeft: null
});

const mainStack = createStackNavigator({

InsideApp: {
    screen: MainScreen,

},
StartScreen: {
    screen: RootStack1,
},
} ,{
headerMode: 'none',
headerLeft: null
},);

const AppNavigator = createAppContainer(mainStack);
export default AppNavigator;

Now My Modal screen will pop up on click of third tab (SurveyScreen) , to achieve that inside surveyscreen all i had to do was override didmount function and open modal from there -

 componentDidMount() {
     console.log('didmount called.');
     this.props.navigation.navigate('CaptureModal')         
 }

For back navigation from modal to tab activity i used stackreset -

const resetAction = StackActions.reset({
 index: 0,
 actions: [NavigationActions.navigate({ routeName: 'TabNavigator' })],
});

and click of back -

onPress={ () => this.props.navigation.dispatch(resetAction)


来源:https://stackoverflow.com/questions/54024006/open-a-modal-on-bottomtabnavigator-click-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!