1

I am building my first react-native app, and Implementing tabs using react-native-tabview. Stuck with the error :

"TypeError: undefined is not an object (evaluating '_this.props.navigationState.routes.length').

This is a screenshot of error I'm getting.

screenshot

import * as React from 'react';
import {
  Platform, StyleSheet, Text, View, Dimensions, StatusBar, FlatList, ImageBackground, TextInput
} from 'react-native';
import { TabView, SceneMap } from 'react-native-tab-view';

const FirstRoute = () => (
  <View style={[styles.scene, { backgroundColor: '#ff4081' }]} />
);

const SecondRoute = () => (
  <View style={[styles.scene, { backgroundColor: '#673ab7' }]} />
);

export default class App extends React.Component {
  state = {
    index: 0,
    routes: [
      { key: 'first', title: 'First' },
      { key: 'second', title: 'Second' },
    ],
  };

  render() {
    return (
      <View style={styles.container}>
        <TabView
          navigationState={this.state}
          renderScene={SceneMap({
            first: FirstRoute,
            second: SecondRoute,
          })}
          onIndexChange={index => this.setState({ index })}
          initialLayout={{ width: Dimensions.get('window').width }}
          style={styles.container}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    marginTop: StatusBar.currentHeight
  },

  scene: {
    flex: 1,
  },
});
shim
  • 9,289
  • 12
  • 69
  • 108

1 Answers1

1

So I copy/pasted and ran your code (with a different background color) as an expo snack here: https://snack.expo.io/B1-xKYu2N and it's working.

If this is your first project, the most likely issue is missing or incorrectly installed packages. Double check your package.json for something like "react-native-tab-view": "^2.0.1". If it's there (or after you add it), try running rm -rf ./node_modules && npm install in terminal from inside the project directory to delete the packages and re-install them. Wish I could be more help!

Rob Bruce
  • 11
  • 1
  • I am facing the same issue but the issue is not resolved can you please help me on this as *try running rm -rf ./node_modules && npm install in the terminal from inside the project directory to delete the packages and re-install them* not able to understood – Ajay Panchal Feb 27 '20 at 09:37