2017-12-18 141 views
0

似乎TabNavigator没有它自己的状态。有什么方法可以使用stateprops国家在TabStackNavigator?

我想在Notification TabIcon上显示the number of unread notification

export default TabNavigator(
    { 
    ... 
    Noti: { 
     screen: NotificationStackNavigator, 
    }, 
    ... 
    }, 
    { 
    navigationOptions: ({ navigation }) => ({ 
     header: null, 
     tabBarIcon: ({ focused }) => { 
     const { routeName } = navigation.state; 
     let iconName; 
     switch (routeName) { 
      ... 
      case 'Noti': 
      iconName = 'ios-notifications'; 
      break; 
      ... 
     } 
     ... 
     if(iconName == 'ios-notifications') { 
      return (
      <IconBadge 
       MainElement={ 
       <Ionicons 
        name={iconName} 
        size={30} 
        style={{ marginBottom: -3 }} 
        color={focused ? Colors.tabIconSelected : Colors.tabIconDefault}/> 
       } 
       BadgeElement={ 
       <Text style={{color:'#FFFFFF'}}> 
        {{this.state.notifications}} // my goal 
       </Text> 
       } 
       IconBadgeStyle={{ 
       backgroundColor: '#f64e59', 
       position: 'absolute', 
       right:-10, 
       top:0, 
       }} 
      /> 
     ); 
     } 
     ... 

请让我知道,如果有什么不清楚。在此先感谢

更新我打算重构我的TabNavigator。这是你想说的吗?

export default TabNavigator( 

to 

const MainTabNavigator = TabNavigator(

class MainTabNavigator extends Component { 
    state = {notification} 

export default connect(...)(MainTabNavigator); 

更新2 MainTabNavigator的顶级组件是另一个TabNavigator的。还有其他解决方案吗?

const RootTabNavigator = TabNavigator ({ 
    Auth: { 
     screen: AuthStackNavigator, 
    }, 
    Welcome: { 
     screen: WelcomeScreen, 
    }, 
    Main: { 
     screen: MainTabNavigator, 
    }, 

回答

1

您可以通过screenprops

const TabNav = TabNavigator({ 
    // config 
}); 

<TabNav 
    screenProps={/* this prop will get passed to the screen components as this.props.screenProps */} 
/> 

完整的文档是here

+0

感谢您的回答,但我不认为你明白我的问题通过其他自定义道具。我想在TabNavigator中使用状态和道具,您在其中评论为'// config'。抱歉,您的示例代码对我没有意义。 –

+0

那么'TabNavigator'是无状态的,所以没有你没有得到状态,但你会得到道具。在你的配置中,你可以引用'this.props.myCustomProp',并且当你渲染时,''TabNav myCustomProp ='在父'/>'上设置一些值 –

+0

我希望这不是真的。我没有办法跟踪'有多少未读通知用户' –