Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Explain navigating between screens in Navigation Guide #15352

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion docs/Navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ npm install --save react-navigation
Then you can quickly create an app with a home screen and a profile screen:

```
/* from App.js */
import {
StackNavigator,
} from 'react-navigation';
Expand All @@ -39,11 +40,30 @@ const App = StackNavigator({
Profile: { screen: ProfileScreen },
});
```
Now we need HomeScreen and ProfileScreen components to be imported. We will be making these two components in a separate folder `components` in root dir.
After importing StackNavigator from react-navigation module and screens from `components` folder your `App.js` file will be:
```
import React from 'react';
import { StackNavigator } from 'react-navigation';
import HomeScreen from './components/HomeScreen';
import ProfileScreen from './components/ProfileScreen';

const App = StackNavigator({
Home: { screen: HomeScreen },
Profile: { screen: ProfileScreen }
});

export default App;
```

Each screen component can set navigation options such as the header title. It can use action creators on the `navigation` prop to link to other screens:

```
class HomeScreen extends React.Component {
/* from ./components/HomeScreen.js */
import React from 'react';
import { Button } from 'react-native';

export default class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
Expand All @@ -60,6 +80,22 @@ class HomeScreen extends React.Component {
}
}
```
Now it's time to jump to Profile Screen
```
/* from ./components/ProfileScreen.js */
import React from 'react';
import { Text } from 'react-native';

export default class ProfileScreen extends React.Component {
static navigationOptions = {
title: 'Profile',
};
render(){
const { state } = this.props.navigation;
return <Text>Hello, {state.params.name}!</Text>
}
}
```

React Navigation routers make it easy to override navigation logic or integrate it into redux. Because routers can be nested inside each other, developers can override navigation logic for one area of the app without making widespread changes.

Expand Down