动态响应本机更改抽屉标签

时间:2019-09-11 23:32:10

标签: react-native react-navigation

我正在使用react-navigation,并且有一个应用程序,如果用户更改了特权(是否为主持人),我需要能够动态更改drawerLabel

我在组件中设置了抽屉标签

  static navigationOptions = {
    title: 'Browse',
    drawerLabel: 'My Bookings',
    drawerIcon: ({tintColor}) => <Image source={navIcon} style={[styles.icon, {tintColor}]} />,
    header: null,
  };

我本以为可以使用setPrams更改标签,但是以下内容似乎无效:

  static getDerivedStateFromProps(props, state) {
    if (props.admin) {
      props.navigation.setParams({drawerLabel: 'Manage Bookings'});
    }
  }

我将如何更改drawerLabel

1 个答案:

答案 0 :(得分:1)

保留此代码:

static getDerivedStateFromProps(props, state) {
   if (props.admin) {
     props.navigation.setParams({drawerLabel: 'Manage Bookings'});
   }
}

并尝试更改此内容:

static navigationOptions = {
  ...
  drawerLabel: 'My Bookings',
  ...
};

对此:

static navigationOptions = ({ navigation }) => {
  const { params } = navigation.state; 
  return {
    ...
    drawerLabel:  params.drawerLabel || 'My Bookings',
    ...
  }
};