2 February 2020

Navigation in React Native


  • Install react-navigation in project as below image
    • npm install react-navigation
    • npx expo-cli install react-native-gesture-handler react-native-reanimated react-navigation-stack

  • Add src folder
  • Add screens folder under src
  • Add HomeScreen.js file under screens folder & add below code in that file
import React from 'react';
import { StyleSheetTextView } from 'react-native';

export default function HomeScreen() {
  return (
    <View style={styles.container}>
      <Text>Adi here...</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});
  • Go to App.js file and update below code
import { createStackNavigatorcreateAppContainer } from 'react-navigation';
import HomeScreen from './src/screens/HomeScreen'

const navigator = createStackNavigator(
  {
    Home: HomeScreen
  },
  {
    initialRouteName: 'Home',
    defaultNavigationOptions: {
      title: 'App'
    }
  }
);

export default createAppContainer(navigator);
  • By using above code, when you start application it will go to HomeScreen.

No comments:

Post a Comment