--- id: basic-navigation title: Basic navigation sidebar_label: Basic navigation --- import useBaseUrl from '@docusaurus/useBaseUrl'; Generally, any mobile app consists of various destinations which display some content to the user. And in vast majority of cases using an app means navigating between these destinations. React-native-navigation provides ways for you to layout your content on user screen in a logical and performant manner. React Native Navigation's stack layout lets you push screens, and also navigate back to previous screens. Screens pushed into the stack hide the previous screen in the stack, making the user focus on a single screen at a time. Let's look at a stack layout that provides basic navigation for an app. ## Creating a stack ```jsx // In index.js of a new project const { Navigation } = require('react-native-navigation'); const React = require('react'); const { View, Text, StyleSheet } = require('react-native'); const HomeScreen = (props) => { return ( Home Screen ); }; Navigation.registerComponent('Home', () => HomeScreen); Navigation.events().registerAppLaunchedListener(async () => { Navigation.setRoot({ root: { stack: { children: [ { component: { name: 'Home' } } ] } } }); }); const styles = StyleSheet.create({ root: { flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: 'whitesmoke' } }); ``` Running this code will render a screen with an empty TopBar and your HomeScreen component (shown below). The TopBar is part of the stack layout, we'll work on configuring it later. ## Specifying options You can specify options of each layout (Stack, component pushed into a stack, etc.) to configure various parameters like the TopBar title or color. Lets spice things up by changing the TopBar color and while we're at it, also move the title to the TopBar. Lets change the Home screen declaration to the following and reload the app to see the changes. ```jsx const HomeScreen = (props) => { return ( Hello React Native Navigation 👋 ); }; HomeScreen.options = { topBar: { title: { text: 'Home', color: 'white' }, background: { color: '#4d089a' } } } ``` Our app should now look like this: ## Navigating in a stack In the previous section we created a stack and initialized it with a single child. We'll now learn how to add another child to the stack. From now on, we'll call the action of adding children to the stack 'push', and removing children - 'pop'. In order to push another screen to the stack, we will add a button to the home screen and call `Navigation.push`. The 'push' command accepts two parameters, the first is the id used to indicate into which stack to push the screen and the second is the screen we're pushing. After pushing a screen, a back button is added automatically to the TopBar so the users can navigate easily back to the previous screen. You can read more about the stack layout [here](stack.mdx) or dive right into the API [here](../api/layout-stack). :::info componentId Each React component registered with `Navigation.registerComponent` is assigned a unique `componentId` by Navigation. This unique id is used with Navigation commands (like the push command) to indicate into which stack we'd like to push. In this case, by using the componentId of the Home screen, we are telling Navigation to push into the stack containing the Home screen. ::: ```js Navigation.push(props.componentId, { component: { name: 'Settings', // Push the screen registered with the 'Settings' key options: { // Optional options object to configure the screen topBar: { title: { text: 'Settings' // Set the TopBar title of the new Screen } } } } }); ``` Lets change our code to the following snippet below and reload the app. Now our Home should contain a button which when pressed, pushes a new screen into the stack. We've successfully navigated to a new screen! 👏 ```jsx // In index.js of a new project const { Navigation } = require('react-native-navigation'); const React = require('react'); const { View, Text, Button, StyleSheet } = require('react-native'); // Settings screen declaration - this is the screen we'll be pushing into the stack const SettingsScreen = () => { return ( Settings Screen ); }; // Home screen declaration const HomeScreen = (props) => { return ( Hello React Native Navigation 👋