Nasir Karbalaei
Nasir Karbalaei

Reputation: 39

TabNavigator only showing first tab

I'm developing a new app with RN and using TabNavigator from react-navigation library, the most basic example of TabNavigator only showing first Tab. I've read somewhere that it could be a bug and could be solved by downgrading react-navigation to 1.0.3 but it didn't work for me. How to solve it?

tab1 tab2

app.js

import React, { Component } from 'react';
import Dashboard from './screens/Dashboard';
import Profile from './screens/Profile';
// import {I18nManager} from 'react-native';
// import { Container, Header, Content, Footer, FooterTab, Button, Icon, Text, Badge, Tab, Tabs } from 'native-base';
import { TabNavigator, TabBarBottom } from 'react-navigation';
import Ionicons from 'react-native-vector-icons/Ionicons';

export default TabNavigator({
    home: { screen: Dashboard },
    profile: { screen: Profile },
    nav: { screen: Dashboard },
},

{
    navigationOptions: ({ navigation }) => ({
        tabBarIcon: ({ focused, tintColor }) => {
            const { routeName } = navigation.state;
            let iconName;
            if (routeName === 'home') {
                iconName = `ios-pulse${focused ? '' : '-outline'}`;
            } else if (routeName === 'profile') {
                iconName = `ios-person${focused ? '' : '-outline'}`;
            }
            // You can return any component that you like here! We usually use an
            // icon component from react-native-vector-icons
            return <Ionicons name={iconName} size={25} color={tintColor} />;
        },
    }),
    tabBarOptions: {
        activeTintColor: 'blue',
        inactiveTintColor: 'gray',
    },
    tabBarComponent: TabBarBottom,
    lazy: false,
    tabBarPosition: 'bottom',
    animationEnabled: false,
    swipeEnabled: false,
}
);

Dashboard.js

import React, { Component } from 'react';
import { TouchableOpacity,
    Title,
    Subtitle,
    Tile,
    Divider,
    ImageBackground,
    Card,
    Image,
    View,
    Caption,
    GridRow,
    ListView,
    Screen
} from '@shoutem/ui';
// import I18n from 'react-native-i18n';
// import {I18nManager} from 'react-native';
//          I18nManager.forceRTL(true);

export default class Dashboard extends Component {
    constructor(props) {
        super(props);
        this.renderRow = this.renderRow.bind(this);
        this.state = {
            restaurants: [
                {
                    'name': 'برنامه ۳۰ روزه هوازی',
                    'address': 'چربی سوزی | کاهش وزن',
                    'image': { 'url': 'https://shoutem.github.io/static/getting-started/restaurant-1.jpg' },
                },
                {
                    'name': 'تمرین سینه',
                    'address': 'افزایش قدرت و حجم عضلات سینه و فرم دهی به آن',
                    'image': { 'url': 'https://shoutem.github.io/static/getting-started/restaurant-2.jpg' },
                },
                {
                    'name': 'تمرین شکم',
                    'address': 'حاضرید که عضلات شکمتان را ورزیده و تکه کنید؟ حرکاتی که در زیر آمده، راهنمایی است که همیشه برای شما کافی و مفید خواهد بود.',
                    'image': { 'url': 'https://shoutem.github.io/static/getting-started/restaurant-3.jpg' },
                },
                {
                    'name': 'تمرین سینه',
                    'address': 'افزایش قدرت و حجم عضلات سینه و فرم دهی به آن',
                    'image': { 'url': 'https://shoutem.github.io/static/getting-started/restaurant-2.jpg' },
                },
                {
                    'name': 'تمرین شکم',
                    'address': 'حاضرید که عضلات شکمتان را ورزیده و تکه کنید؟ حرکاتی که در زیر آمده، راهنمایی است که همیشه برای شما کافی و مفید خواهد بود.',
                    'image': { 'url': 'https://shoutem.github.io/static/getting-started/restaurant-3.jpg' },
                },
                {
                    'name': 'تمرین ران پا',
                    'address': 'این یک تست است.',
                    'image': { 'url': 'https://shoutem.github.io/static/getting-started/restaurant-2.jpg' },
                },
            ],
        };
    }

    renderRow(rowData, sectionId, index) {
    // rowData contains grouped data for one row,
    // so we need to remap it into cells and pass to GridRow
        if (index === '0') {
            return (
                <TouchableOpacity key={index}>
                    <ImageBackground
                        styleName="large"
                        source={{ uri: rowData[0].image.url }}
                    >
                        <Tile>
                            <Title styleName="md-gutter-bottom">{rowData[0].name}</Title>
                            <Subtitle styleName="sm-gutter-horizontal">{rowData[0].address}</Subtitle>
                        </Tile>
                    </ImageBackground>
                    <Divider styleName="line" />
                </TouchableOpacity>
            );
        }

        const cellViews = rowData.map((restaurant, id) => {
            return (
                <TouchableOpacity key={id} styleName="flexible">
                    <Card styleName="flexible">
                        <Image
                            styleName="medium-wide"
                            source={{ uri: restaurant.image.url  }}
                        />
                        <View styleName="content">
                            <Subtitle numberOfLines={3}>{restaurant.name}</Subtitle>
                            <View styleName="horizontal">
                                <Caption styleName="collapsible" numberOfLines={2}>{restaurant.address}</Caption>
                            </View>
                        </View>
                    </Card>
                </TouchableOpacity>
            );
        });

        return (
            <GridRow columns={2}>
                {cellViews}
            </GridRow>
        );
    }

    render() {
        const restaurants = this.state.restaurants;
        // Group the restaurants into rows with 2 columns, except for the
        // first restaurant. The first restaurant is treated as a featured restaurant
        let isFirstArticle = true;
        const groupedData = GridRow.groupByRows(restaurants, 2, () => {
            if (isFirstArticle) {
                isFirstArticle = false;
                return 2;
            }
            return 1;
        });

        return (
            <ListView
                data={groupedData}
                renderRow={this.renderRow}
            />
        );
    }
}

Profile.js

import React, { Component } from 'react';
import { Container, Header, Content, Form, Item, Input, Label } from 'native-base';

// import I18n from 'react-native-i18n';
// import {I18nManager} from 'react-native';
//          I18nManager.forceRTL(true);

export default class Profile extends Component {
    render() {
        return (
            <Container>
                <Header />
                <Content>
                    <Form>
                        <Item floatingLabel>
                            <Label>نام</Label>
                            <Input />
                        </Item>
                        <Item floatingLabel last>
                            <Label>قد (سانتیمتر)</Label>
                            <Input />
                        </Item>
                    </Form>
                </Content>
            </Container>
        );
    }
}

package.json

  "dependencies": {
    "@shoutem/ui": "^0.23.4",
    "native-base": "^2.4.2",
    "react": "16.3.1",
    "react-native": "0.55.3",
    "react-native-vector-icons": "^4.6.0",
    "react-navigation": "^1.0.3"
  },
  "devDependencies": {
    "babel-jest": "22.4.3",
    "babel-preset-react-native": "4.0.0",
    "eslint": "^4.19.1",
    "eslint-plugin-react": "^7.7.0",
    "jest": "22.4.3",
    "react-test-renderer": "16.3.1"
  },
  "jest": {
    "preset": "react-native"
  }

I've already tried latest version of react-navigation so its downgraded version you see in package.json

Upvotes: 1

Views: 1494

Answers (2)

Nasir Karbalaei
Nasir Karbalaei

Reputation: 39

I've found solution to this, and I'm going to be as specific as I can for all the people out there that might face this!

First of all it's a problem with I18nManager.forceRTL(true); the moment you use this line of code anywhere on your react-native code that its screen gonna get rendered, and on 2nd reload, the app the layout is being change to RTL, and it does not change even if you comment that line! You'll have to use I18nManager.forceRTL(false); and reload a couple of times to get back to normal ltr setup.

The thing is... some of us really need that RTL layout change like I thought I did! Well guess what react-navigation does not respect that, at least for now and in the TabNabigator dept.

So to sum it all up: RTL layout on RN will break your react-navigation's Tab Navigation! (as of the current version you can see in the packages.json above) The issue makes only the first that visible and for the others a bland tab shows, if you turn on the animation or swipe other tabs will show but tabs behave in a weird way, meaning the last tab is active when the first one is active and vice versa... middle tabs are never focused by the way. So you should know you can't use tabbed navigation with RTL layout. I'll update this answer after this issue got a fix!

Upvotes: 2

Shamar Yarde
Shamar Yarde

Reputation: 761

I have run it using react-native run-ios, and all the tabs display a different screen. If you are referring to the fact that the nav tab does not change when you click on it whilst on the home tab, both the home and nav tabs are using the Dashboard screen.

The following is my package.json file for this project:

{
  "name": "a",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react-native-code-push": "1.15.0-beta",
    "@babel/core": "^7.0.0-beta.40",
    "@shoutem/ui": "^0.23.4",
    "eslint": "^3.19.0",
    "native-base": "^2.4.2",
    "react": "16.3.1",
    "react-native": "0.55.3",
    "react-native-vector-icons": "^4.6.0",
    "react-navigation": "^1.5.11"
  },
  "devDependencies": {
    "babel-jest": "22.4.3",
    "babel-preset-react-native": "4.0.0",
    "jest": "22.4.3",
    "react-test-renderer": "16.3.1"
  },
  "jest": {
    "preset": "react-native"
  }
}

Upvotes: 1

Related Questions