如何将道具传递给BottomTabNavigator中的函数

时间:2018-10-09 19:11:14

标签: react-native react-redux react-navigation react-native-navigation react-native-tabnavigator

我已经在SO中进行了很多搜索,但没有找到解决我的问题的可行方法,

问题陈述:我有5个选项卡的BottomTabNavigator。每次我切换标签时,我都需要进行api调用以获取最新信息。为了获取最新信息,我需要具有用于进行api调用的redux存储值。那么,如何使这些redux存储值在我在tabChange上进行的函数调用中可用?

下面是我的函数,需要先调用它,然后调用BottomTabNavigator,

    fetchProfile = () => {
        const {selectedPerson, dispatch, actions} = this.props
        let Id = selectedPerson.Id
        let personId = selectedPerson.placeId
        dispatch(actions.getPersonDetails(Id, personId))
    }

    export const Tabs = createBottomTabNavigator({
        Profile: { 
            screen: ProfileConnector,
            navigationOptions: {
                tabBarLabel: 'Profile',
                tabBarIcon: ({tintColor}) => <Icon name="user" size={px2dp(22)} color={tintColor} onPress={() => fetchProfile()}/>,
            },
            // navigationOptions: ({ navigation }) => ({
            //     tabBarOnPress: (tab, jumpToIndex) => {
            //       console.log('onPress:', tab.route);
            //       jumpToIndex(tab.index);
            //     },
            //     tabBarLabel: 'Profile',
            //     tabBarIcon: ({tintColor}) => <Icon name="user" size={px2dp(22)} color={tintColor}/>,
            //   }),
        },
        Inbox: { 
            screen: InboxConnector,
            navigationOptions: {
                tabBarLabel: 'Inbox',
                tabBarIcon: ({tintColor}) => <Icon name="inbox" size={px2dp(22)} color={tintColor}/>,
            },
        },
        Bling: {
            screen: BlingConnector,
            navigationOptions: {
                tabBarLabel: 'Bling',
                tabBarIcon: ({tintColor}) => <Icon name="hand" size={px2dp(22)} color={tintColor}/>,
            },
        },
        Calendar: {
            screen: CalendarConnector,
            navigationOptions: {
                tabBarLabel: 'Calendar',
                tabBarIcon: ({tintColor}) => <Icon name="calendar" size={px2dp(22)} color={tintColor}/>,
            },
        },
        Misc: {
            screen: Misc,
            navigationOptions: {
                tabBarLabel: 'Misc',
                tabBarIcon: ({tintColor}) => <Icon name="bar-graph" size={px2dp(22)} color={tintColor}/>,
            },
        },
    }, {
        initialRouteName: 'Inbox',
        tabBarPosition: 'bottom',
        swipeEnabled: true,
        scrollEnabled: true,
        tabBarOptions: {
            activeTintColor: 'teal',
            inactiveTintColor: '#424949',
            activeBackgroundColor: "white",
            inactiveTintColor: '#424949',
            labelStyle: { fontSize: 14 },
            style : { height : 50},
        }
    });

任何帮助都将不胜感激,我对此感到迷mind。 我已经尝试了上面的注释掉的部分,并抛出了jumpToIndex is not a function 并且另一个选项没有redux存储值。

请帮助。 :( 谢谢, 维克拉姆

1 个答案:

答案 0 :(得分:0)

“每次切换标签时,我都需要进行api调用以获取最新信息。”

您可以检测到willFocus / didFocus的反应导航事件,然后获取api

react-navigation: Detect when screen is activated / appear / focus / blur