In react navigation we cannot have dynamic tab, as we have to define all the routes statically well in advance.
We had such requirement too, so we followed the below mentioned approach.
- Define all the possible tabs statically and make tabs invisible.
const MainNav = createBottomTabNavigator(
{
Breads: { screen: BreadsStackNavigation },
Pizzas: { screen: PizzasStackNavigation },
Beverages: { screen: BeveragesStackNavigation },
Desserts: { screen: DessertsStackNavigation },
Shakes: { screen: ShakesStackNavigation }
},
{
initialRouteName: 'HomeStackNavigation',
defaultNavigationOptions: {
tabBarVisible: false,
},
swipeEnabled: false,
navigationOptions: {
header: props => <HeaderView
navigation={props.navigation}
/>,
tabBarVisible: false
}
}
);
- In the HeaderView, in
componentWillReceiveProps
check the response from the server and accordingly create tabs dynamically (may be by adding buttons dynamically depending on the responses).
UPDATE
I am thinking about one more approach.
As given in this link we can create our own custom navigator, and populate the tabs dynamic in the parent custom navigator after getting response from the API call. I do not have the exact code for the same, but you can try this approach.