Skip to content

Commit 5d9ec02

Browse files
committed
Rework fundamentals to accomodate static API
1 parent 4806039 commit 5d9ec02

15 files changed

+285
-52
lines changed

static/examples/7.x/go-back.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from 'react';
22
import { Button, View, Text } from 'react-native';
3-
import { NavigationContainer } from '@react-navigation/native';
3+
import { NavigationContainer, useNavigation } from '@react-navigation/native';
44
import { createNativeStackNavigator } from '@react-navigation/native-stack';
55

6-
function HomeScreen({ navigation }) {
6+
function HomeScreen() {
7+
const navigation = useNavigation();
8+
79
return (
810
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
911
<Text>Home Screen</Text>
@@ -15,7 +17,9 @@ function HomeScreen({ navigation }) {
1517
);
1618
}
1719

18-
function DetailsScreen({ navigation }) {
20+
function DetailsScreen() {
21+
const navigation = useNavigation();
22+
1923
return (
2024
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
2125
<Text>Details Screen</Text>

static/examples/7.x/multiple-navigate.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from 'react';
22
import { Button, View, Text } from 'react-native';
3-
import { NavigationContainer } from '@react-navigation/native';
3+
import { NavigationContainer, useNavigation } from '@react-navigation/native';
44
import { createNativeStackNavigator } from '@react-navigation/native-stack';
55

6-
function HomeScreen({ navigation }) {
6+
function HomeScreen() {
7+
const navigation = useNavigation();
8+
79
return (
810
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
911
<Text>Home Screen</Text>
@@ -15,7 +17,9 @@ function HomeScreen({ navigation }) {
1517
);
1618
}
1719

18-
function DetailsScreen({ navigation }) {
20+
function DetailsScreen() {
21+
const navigation = useNavigation();
22+
1923
return (
2024
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
2125
<Text>Details Screen</Text>

static/examples/7.x/multiple-push.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from 'react';
22
import { Button, View, Text } from 'react-native';
3-
import { NavigationContainer } from '@react-navigation/native';
3+
import { NavigationContainer, useNavigation } from '@react-navigation/native';
44
import { createNativeStackNavigator } from '@react-navigation/native-stack';
55

6-
function HomeScreen({ navigation }) {
6+
function HomeScreen() {
7+
const navigation = useNavigation();
8+
79
return (
810
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
911
<Text>Home Screen</Text>
@@ -15,7 +17,9 @@ function HomeScreen({ navigation }) {
1517
);
1618
}
1719

18-
function DetailsScreen({ navigation }) {
20+
function DetailsScreen() {
21+
const navigation = useNavigation();
22+
1923
return (
2024
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
2125
<Text>Details Screen</Text>

static/examples/7.x/new-screen.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from 'react';
22
import { Button, View, Text } from 'react-native';
3-
import { NavigationContainer } from '@react-navigation/native';
3+
import { NavigationContainer, useNavigation } from '@react-navigation/native';
44
import { createNativeStackNavigator } from '@react-navigation/native-stack';
55

6-
function HomeScreen({ navigation }) {
6+
function HomeScreen() {
7+
const navigation = useNavigation();
8+
79
return (
810
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
911
<Text>Home Screen</Text>

static/examples/7.x/params-nested-navigators.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import * as React from 'react';
22
import { Button, View, Text } from 'react-native';
3-
import { NavigationContainer } from '@react-navigation/native';
3+
import { NavigationContainer, useNavigation } from '@react-navigation/native';
44
import { createNativeStackNavigator } from '@react-navigation/native-stack';
55
import { createDrawerNavigator } from '@react-navigation/drawer';
66

7-
function SettingsScreen({ route, navigation }) {
7+
function SettingsScreen({ route }) {
8+
const navigation = useNavigation();
9+
810
const { user } = route.params;
911
return (
1012
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
@@ -18,15 +20,19 @@ function SettingsScreen({ route, navigation }) {
1820
);
1921
}
2022

21-
function ProfileScreen({ navigation }) {
23+
function ProfileScreen() {
24+
const navigation = useNavigation();
25+
2226
return (
2327
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
2428
<Text>Profile Screen</Text>
2529
</View>
2630
);
2731
}
2832

29-
function HomeScreen({ navigation }) {
33+
function HomeScreen() {
34+
const navigation = useNavigation();
35+
3036
return (
3137
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
3238
<Text>Home Screen</Text>

static/examples/7.x/passing-params-back.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from 'react';
22
import { Text, TextInput, View, Button } from 'react-native';
3-
import { NavigationContainer } from '@react-navigation/native';
3+
import { NavigationContainer, useNavigation } from '@react-navigation/native';
44
import { createNativeStackNavigator } from '@react-navigation/native-stack';
55

6-
function HomeScreen({ navigation, route }) {
6+
function HomeScreen({ route }) {
7+
const navigation = useNavigation();
8+
79
React.useEffect(() => {
810
if (route.params?.post) {
911
// Post updated, do something with `route.params.post`
@@ -22,7 +24,8 @@ function HomeScreen({ navigation, route }) {
2224
);
2325
}
2426

25-
function CreatePostScreen({ navigation, route }) {
27+
function CreatePostScreen({ route }) {
28+
const navigation = useNavigation();
2629
const [postText, setPostText] = React.useState('');
2730

2831
return (

static/examples/7.x/passing-params.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from 'react';
22
import { Text, View, Button } from 'react-native';
3-
import { NavigationContainer } from '@react-navigation/native';
3+
import { NavigationContainer, useNavigation } from '@react-navigation/native';
44
import { createNativeStackNavigator } from '@react-navigation/native-stack';
55

6-
function HomeScreen({ navigation }) {
6+
function HomeScreen() {
7+
const navigation = useNavigation();
8+
79
return (
810
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
911
<Text>Home Screen</Text>
@@ -21,7 +23,9 @@ function HomeScreen({ navigation }) {
2123
);
2224
}
2325

24-
function DetailsScreen({ route, navigation }) {
26+
function DetailsScreen({ route }) {
27+
const navigation = useNavigation();
28+
2529
/* 2. Get the param */
2630
const { itemId } = route.params;
2731
const { otherParam } = route.params;

static/examples/7.x/pop-to-top.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import * as React from 'react';
22
import { Button, View, Text } from 'react-native';
3-
import { NavigationContainer } from '@react-navigation/native';
3+
import { NavigationContainer, useNavigation } from '@react-navigation/native';
44
import { createNativeStackNavigator } from '@react-navigation/native-stack';
55

6-
function HomeScreen({ navigation }) {
6+
function HomeScreen() {
7+
const navigation = useNavigation();
8+
79
return (
810
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
911
<Text>Home Screen</Text>
@@ -15,7 +17,9 @@ function HomeScreen({ navigation }) {
1517
);
1618
}
1719

18-
function DetailsScreen({ navigation }) {
20+
function DetailsScreen() {
21+
const navigation = useNavigation();
22+
1923
return (
2024
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
2125
<Text>Details Screen</Text>

versioned_docs/version-7.x/getting-started.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,25 @@ When you use a navigator (such as stack navigator), you'll need to follow the in
114114

115115
:::
116116

117-
## Wrapping your app in `NavigationContainer`
117+
## Setting up React Navigation
118118

119-
Now, we need to wrap the whole app in `NavigationContainer`. Usually you'd do this in your entry file, such as `index.js` or `App.js`:
119+
Once you've installed and configured the dependencies, you can move on to setting up your project to use React Navigation.
120+
121+
When using React Navigation, you configure [**navigators**](glossary-of-terms.md#navigator) in your app. Navigators handle the transition between screens in your app and provide UI such as header, tab bar etc.
122+
123+
There are 2 primary ways to configure the navigators:
124+
125+
### Static configuration
126+
127+
The static configuration API has reduced boilerplate and simplifies things such as TypeScript types and deep linking. If you're staring a new project or are new to React Navigation, this is the **recommended way** to set up your app.
128+
129+
To get started with static configuration, continue to ["Getting started with static API"](hello-react-navigation-static.md).
130+
131+
### Dynamic configuration
132+
133+
The dynamic configuration allows for more flexibility, but requires more boilerplate and configuration.
134+
135+
To get started with dynamic configuration, first we need to wrap your app in `NavigationContainer`. Usually you'd do this in your entry file, such as `index.js` or `App.js`:
120136

121137
```js
122138
import * as React from 'react';
@@ -135,6 +151,4 @@ In a typical React Native app, the `NavigationContainer` should be only used onc
135151

136152
:::
137153

138-
Now you are ready to build and run your app on the device/simulator.
139-
140-
Continue to ["Hello React Navigation"](hello-react-navigation.md) to start writing some code.
154+
Continue to ["Getting started with dynamic API"](hello-react-navigation.md) to start writing some code.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
id: hello-react-navigation-static
3+
title: Static configuration
4+
sidebar_label: Static configuration
5+
---
6+
7+
In a web browser, there are multiple **pages** and you can **link to** the pages using an anchor (`<a>`) tag. Similarly, in React Native, you have multiple **screens** and you can **navigate** between them.
8+
9+
React Navigation provides different types of navigators, such as the stack navigator, tab navigator, drawer navigator, etc. Each navigator provides a different way of transitioning between screens. They have a similar API for configuring them.
10+
11+
The stack navigator is the most commonly used navigator. It resembles how you would expect multiple pages to work in a web browser. In this guide, let's start by demonstrating how to configure the native stack navigator.
12+
13+
## Installing the native stack navigator library
14+
15+
The libraries we've installed so far are the building blocks and shared foundations for navigators, and each navigator in React Navigation lives in its own library. To use the native stack navigator, we need to install [`@react-navigation/native-stack`](https://github.com/react-navigation/react-navigation/tree/main/packages/native-stack) :
16+
17+
```bash npm2yarn
18+
npm install @react-navigation/native-stack@next
19+
```
20+
21+
> 💡 `@react-navigation/native-stack` depends on `react-native-screens` and the other libraries that we installed in [Getting started](getting-started.md). If you haven't installed those yet, head over to that page and follow the installation instructions.
22+
23+
## Creating a native stack navigator
24+
25+
`createNativeStackNavigator` is a function that takes a configuration object containing the screens and customization options. The screens are React Components that render the content displayed by the navigator.
26+
27+
```js
28+
// In App.js in a new project
29+
30+
import * as React from 'react';
31+
import { View, Text } from 'react-native';
32+
import { createStaticNavigation } from '@react-navigation/native';
33+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
34+
35+
function HomeScreen() {
36+
return (
37+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
38+
<Text>Home Screen</Text>
39+
</View>
40+
);
41+
}
42+
43+
const RootStack = createNativeStackNavigator({
44+
screens: {
45+
Home: HomeScreen,
46+
},
47+
});
48+
49+
const Navigation = createStaticNavigation(RootStack);
50+
51+
function App() {
52+
return <Navigation />;
53+
}
54+
55+
export default App;
56+
```
57+
58+
If you run this code, you will see a screen with an empty navigation bar and a grey content area containing your `HomeScreen` component (shown above). The styles you see for the navigation bar and the content area are the default configuration for a stack navigator, we'll learn how to configure those later.
59+
60+
> The casing of the route name doesn't matter -- you can use lowercase `home` or capitalized `Home`, it's up to you. We prefer capitalizing our route names.
61+
62+
### Configuring the navigator
63+
64+
All of the route configuration is specified as props to our navigator. We haven't passed any props to our navigator, so it just uses the default configuration.
65+
66+
Let's add a second screen to our native stack navigator and configure the `Home` screen to render first:
67+
68+
```js
69+
function DetailsScreen() {
70+
return (
71+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
72+
<Text>Details Screen</Text>
73+
</View>
74+
);
75+
}
76+
77+
const RootStack = createNativeStackNavigator({
78+
initialRouteName: 'Home',
79+
screens: {
80+
Home: HomeScreen,
81+
Details: DetailsScreen,
82+
},
83+
});
84+
85+
const Navigation = createStaticNavigation(RootStack);
86+
87+
function App() {
88+
return <Navigation />;
89+
}
90+
```
91+
92+
Now our stack has two _routes_, a `Home` route and a `Details` route. A route can be specified by using the `Screen` component. The `Screen` component accepts a `name` prop which corresponds to the name of the route we will use to navigate, and a `component` prop which corresponds to the component it'll render.
93+
94+
Here, the `Home` route corresponds to the `HomeScreen` component, and the `Details` route corresponds to the `DetailsScreen` component. The initial route for the stack is the `Home` route. Try changing it to `Details` and reload the app (React Native's Fast Refresh won't update changes from `initialRouteName`, as you might expect), notice that you will now see the `Details` screen. Then change it back to `Home` and reload once more.
95+
96+
### Specifying options
97+
98+
Each screen in the navigator can specify some options for the navigator, such as the title to render in the header. To specify the options, we'll change how we have specified the screen component:
99+
100+
```js
101+
const RootStack = createNativeStackNavigator({
102+
initialRouteName: 'Home',
103+
screens: {
104+
Home: {
105+
screen: HomeScreen,
106+
},
107+
Details: DetailsScreen,
108+
},
109+
});
110+
```
111+
112+
Now, we can add an `options` property:
113+
114+
```js
115+
const RootStack = createNativeStackNavigator({
116+
initialRouteName: 'Home',
117+
screens: {
118+
Home: {
119+
screen: HomeScreen,
120+
options: {
121+
title: 'Overview',
122+
}
123+
},
124+
Details: DetailsScreen,
125+
},
126+
});
127+
```
128+
129+
Sometimes we will want to specify the same options for all of the screens in the navigator. For that, we can pass add a `screenOptions` property to the configuration.
130+
131+
```js
132+
const RootStack = createNativeStackNavigator({
133+
initialRouteName: 'Home',
134+
screenOptions: {
135+
headerStyle: { backgroundColor: 'tomato' },
136+
},
137+
screens: {
138+
Home: {
139+
screen: HomeScreen,
140+
options: {
141+
title: 'Overview',
142+
}
143+
},
144+
Details: DetailsScreen,
145+
},
146+
});
147+
```
148+
149+
Here we have specified a `headerStyle` property. This will customize the styles of the header in all of the screens of the navigator.
150+
151+
## What's next?
152+
153+
The natural question at this point is: "how do I go from the `Home` route to the `Details` route?". That is covered in the [navigating section](navigating.md) under fundamentals.
154+
155+
## Summary
156+
157+
- React Native doesn't have a built-in API for navigation like a web browser does. React Navigation provides this for you, along with the iOS and Android gestures and animations to transition between screens.
158+
- `createNativeStackNavigator` is a function that takes the screens configuration and renders our content.
159+
- Each property under screens refers to the name of the route, and the value is the component to render for the route.
160+
- To specify what the initial route in a stack is, provide an `initialRouteName` option for the navigator.
161+
- To specify screen-specific options, we can specify an `options` property, and for common options, we can specify `screenOptions`.

0 commit comments

Comments
 (0)