R
R
Roman Rakzin2020-05-03 18:46:18
React Native
Roman Rakzin, 2020-05-03 18:46:18

How to catch menu click event in React Native Drawer Navigation?

I need to pass a parameter to the screen, but I did not find how to do it. How can I display not just a screen, but a screen with parameters when choosing a certain menu? In the example, I have the option Politics, Economy and Home page - they all lead to one screen, but I need to pass a parameter so that when I click on the policy button in that screen, only articles about politics will be displayed. How to do this?

const PostNavigator = createStackNavigator(
  {
    Main: MainScreen,
    Post: PostScreen
  },
  navigatorOptions
) 

const AboutUsNavigator = createStackNavigator(
  {
    AboutUs: AboutUsScreen
  },
  navigatorOptions
)

const AboutRedNavigator = createStackNavigator(
  {
    AboutRed: AboutRedScreen
  },
  navigatorOptions
)

const MainNavigator = createDrawerNavigator(
  {
    Main: {
      screen: PostNavigator,
      navigationOptions: {
        drawerLabel: 'Главная'
      }
    },
    Category_Politik: {
      screen: PostNavigator,    
      navigationOptions: {
        drawerLabel: 'Политика'
      }
    },
    Category_Ekonomik: {
      screen: PostNavigator,
      navigationOptions: {
        drawerLabel: 'Экономика'
      }
    },
    AboutUs: {
      screen: AboutUsNavigator,
      navigationOptions: {
        drawerLabel: 'Об издании'
      }
    }
  },
  {
    initialRouteName: 'Main',
    drawerBackgroundColor: '#D2D2D2',
  }
)

export const AppNavigation = createAppContainer(MainNavigator)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Alexandrovich, 2020-05-03
@RomReed

Alternatively, you can pass parameters

navigation.navigate('Details', {
            itemId: 42,
            otherParam: 'anything you want here',
          });

or like this
<Stack.Screen
  name="Details"
  component={DetailsScreen}
  initialParams={{ itemId: 42 }}
/>

it's all described https://reactnavigation.org/docs/params take the time to read the documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question