React-native更改堆栈导航器标题基于从嵌套抽屉导航中选择的内容

时间:2020-12-20 18:59:36

I have a drawer navigator nested inside a stack navigator like so

我有一个嵌套在堆栈导航器中的抽屉导航器,就像这样

const DrawerNavigation = DrawerNavigator({
  Home: {
    screen: Home,
    navigationOptions: {
      drawerLabel: 'Home',
      drawerIcon: () => (
        <MaterialCommunityIcons name={'home'} size={25} />
        )
    },
  },
  Customers: {
    screen: Customers,
    navigationOptions: {
      drawerLabel: 'Customers',
      drawerIcon: () => (
        <MaterialCommunityIcons name={'account-switch'} size={25} />
        )
    },
  },
}, {
  drawerPosition: 'right',
})

const Navigation = StackNavigator({
  Home: {
    screen : DrawerNavigation,
    navigationOptions: ({ navigation }) => ({
      title: navigation.state.routeName, // Always 'Home'
    }),
  },
})

This gave me the layout I desired, with a "title bar" that displays on both IOS and android, and makes the drawer navigation the main method of navigating the application. However, I would like the title prop for the stack navigator to display the name of whatever screen is selected from the drawer navigator rather than a static string. Is this possible with this current setup? Perhaps this is the incorrect approach - I am new to react-native's navigation.

这给了我想要的布局,在IOS和android上都显示了“标题栏”,并使抽屉导航成为导航应用程序的主要方法。但是,我希望堆栈导航器的标题道具显示从抽屉导航器中选择的任何屏幕的名称,而不是静态字符串。这种当前设置是否可行?也许这是不正确的方法 - 我是新的反应原生的导航。

1 个解决方案

#1


1  

You can add title for each screen in the navigationOptions for each screen.

您可以在每个屏幕的navigationOptions中为每个屏幕添加标题。

const DrawerNavigation = DrawerNavigator({
  Home: {
    screen: Home,
    navigationOptions: {
      drawerLabel: 'Home',
      title: 'Home, sweet home!' // <=== add this
      drawerIcon: () => (
        <MaterialCommunityIcons name={'home'} size={25} />
        )
    },
  },
  Customers: {
    screen: Customers,
    navigationOptions: {
      drawerLabel: 'Customers',
      title: 'Dear customers', // <=== and this
      drawerIcon: () => (
        <MaterialCommunityIcons name={'account-switch'} size={25} />
        )
    },
  },
}, {
  drawerPosition: 'right',
})

const Navigation = StackNavigator({
  Home: {
    screen : DrawerNavigation,
    navigationOptions: ({ navigation }) => ({
      title: navigation.state.routeName, // <=== remove this
    }),
  },
})

Also, you can set title on each screen component, more details here

此外,您可以在每个屏幕组件上设置标题,此处有更多详细信息

#1


1  

You can add title for each screen in the navigationOptions for each screen.

您可以在每个屏幕的navigationOptions中为每个屏幕添加标题。

const DrawerNavigation = DrawerNavigator({
  Home: {
    screen: Home,
    navigationOptions: {
      drawerLabel: 'Home',
      title: 'Home, sweet home!' // <=== add this
      drawerIcon: () => (
        <MaterialCommunityIcons name={'home'} size={25} />
        )
    },
  },
  Customers: {
    screen: Customers,
    navigationOptions: {
      drawerLabel: 'Customers',
      title: 'Dear customers', // <=== and this
      drawerIcon: () => (
        <MaterialCommunityIcons name={'account-switch'} size={25} />
        )
    },
  },
}, {
  drawerPosition: 'right',
})

const Navigation = StackNavigator({
  Home: {
    screen : DrawerNavigation,
    navigationOptions: ({ navigation }) => ({
      title: navigation.state.routeName, // <=== remove this
    }),
  },
})

Also, you can set title on each screen component, more details here

此外,您可以在每个屏幕组件上设置标题,此处有更多详细信息