Skip to content

Commit 1347ab8

Browse files
committed
Add doc for web, createAppContainer docs for v3
1 parent 2befa36 commit 1347ab8

File tree

7 files changed

+184
-74
lines changed

7 files changed

+184
-74
lines changed

docs/app-containers.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
id: app-containers
3+
title: App Containers
4+
sidebar_label: App Containers
5+
---
6+
7+
Containers are responsible for managing your app state and linking your top-level navigator to the app environment. On Android, the app container uses the Linking API to handle the back button. The container can also be configured to persist your navigation state. On web, you'd use different containers than React Native.
8+
9+
> Note: In v2 and earlier, the containers in React Navigation were automatically provided by the `create*Navigator` functions. Now you are required to use the container directly. In v3 we also renamed `createNavigationContainer` to `createAppContainer`.
10+
11+
A quick example of `createAppContainer`:
12+
13+
```
14+
import { createAppContainer } from 'react-navigation';
15+
// you can also import from @react-navigation/native
16+
17+
const AppNavigator = createStackNavigator(...);
18+
19+
const AppContainer = createAppContainer(AppNavigator);
20+
21+
// Now AppContainer is the main component for React to render
22+
23+
export default AppContainer;
24+
```
25+
26+
## Props of `createAppContainer` on React Native
27+
28+
```js
29+
<AppContainer
30+
onNavigationStateChange={handleNavigationChange}
31+
uriPrefix="/app"
32+
/>
33+
```
34+
35+
### `onNavigationStateChange(prevState, newState, action)`
36+
37+
Function that gets called every time navigation state managed by the navigator changes. It receives the previous state, the new state of the navigation and the action that issued state change. By default it prints state changes to the console.
38+
39+
### `uriPrefix`
40+
41+
The prefix of the URIs that the app might handle. This will be used when handling a [deep link](deep-linking.html) to extract the path passed to the router.
42+
43+
## Calling Dispatch or Navigate on App Container
44+
45+
In case you want to dispatch actions on an app container, you can use a React [`ref`](https://facebook.github.io/react/docs/refs-and-the-dom.html#the-ref-callback-attribute) to call the `dispatch` method on it:
46+
47+
```js
48+
const AppContainer = createAppContainer(AppNavigator);
49+
50+
class App extends React.Component {
51+
someEvent() {
52+
// call navigate for AppNavigator here:
53+
this.navigator &&
54+
this.navigator.dispatch(
55+
NavigationActions.navigate({ routeName: someRouteName })
56+
);
57+
}
58+
render() {
59+
return (
60+
<AppContainer
61+
ref={nav => {
62+
this.navigator = nav;
63+
}}
64+
/>
65+
);
66+
}
67+
}
68+
```
69+
70+
## App Containers on the web
71+
72+
On the web, you can use `createBrowserApp` and `handleServerRequest` to maintain the state for your top-level navigator.

docs/custom-navigator-overview.md

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,6 @@ Under the hood, navigators are plain React components.
2222
The navigators render application screens which are just React components.
2323

2424
To learn how to create screens, read about:
25+
2526
- [Screen `navigation` prop](navigation-prop.html) to allow the screen to dispatch navigation actions, such as opening another screen
2627
- Screen `navigationOptions` to customize how the screen gets presented by the navigator (e.g. [header title](stack-navigator.html#navigationoptions-used-by-stacknavigator), tab label)
27-
28-
### Calling Navigate on Top Level Component
29-
30-
In case you want to use Navigator from the same level you declare it you can use react's [`ref`](https://facebook.github.io/react/docs/refs-and-the-dom.html#the-ref-callback-attribute) option:
31-
```js
32-
import { NavigationActions } from 'react-navigation';
33-
34-
const AppNavigator = createStackNavigator(SomeAppRouteConfigs);
35-
36-
class App extends React.Component {
37-
someEvent() {
38-
// call navigate for AppNavigator here:
39-
this.navigator && this.navigator.dispatch(
40-
NavigationActions.navigate({ routeName: someRouteName })
41-
);
42-
}
43-
render() {
44-
return (
45-
<AppNavigator ref={nav => { this.navigator = nav; }} />
46-
);
47-
}
48-
}
49-
```
50-
Please notice that this solution should only be used on the top level navigator.
51-
52-
## Navigation Containers
53-
54-
The built in navigators can automatically behave like top-level navigators when the navigation prop is missing. This functionality provides a transparent navigation container, which is where the top-level navigation prop comes from.
55-
56-
When rendering one of the included navigators, the navigation prop is optional. When it is missing, the container steps in and manages its own navigation state. It also handles URLs, external linking, and Android back button integration.
57-
58-
For the purpose of convenience, the built-in navigators have this ability because behind the scenes they use `createNavigationContainer`. Usually, navigators require a navigation prop in order to function.
59-
60-
Top-level navigators accept the following props:
61-
62-
### `onNavigationStateChange(prevState, newState, action)`
63-
64-
Function that gets called every time navigation state managed by the navigator changes. It receives the previous state, the new state of the navigation and the action that issued state change. By default it prints state changes to the console.
65-
66-
### `uriPrefix`
67-
68-
The prefix of the URIs that the app might handle. This will be used when handling a [deep link](deep-linking.html) to extract the path passed to the router.

docs/custom-navigators.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class CustomNavigator extends React.Component {
5252
}
5353
```
5454

55-
5655
## Navigator Navigation Prop
5756

5857
The navigation prop passed down to a navigator only includes `state`, `dispatch`, and `addListener`. This is the current state of the navigator, and an event channel to send action requests.
@@ -102,7 +101,7 @@ To help developers implement custom navigators, the following utilities are prov
102101
This utility combines a [router](routers.html) and a [navigation view](navigation-views.html) together in a standard way:
103102

104103
```js
105-
import {createNavigator} from 'react-navigation';
104+
import { createNavigator } from "react-navigation";
106105

107106
const AppNavigator = createNavigator(NavigationView, router, navigationConfig);
108107
```
@@ -127,7 +126,7 @@ Then the view will be rendered in the following way:
127126
/>
128127
```
129128

130-
The `navigation` prop is the same navigation prop that was passed into the navigator.
129+
The `navigation` prop is the same navigation prop that was passed into the navigator.
131130

132131
Both `navigationConfig` and `screenProps` are simply passed through, as defined above.
133132

@@ -142,8 +141,3 @@ A scene descriptor has the following properties:
142141
- `navigation`, the child navigation prop, including actions and the route `state`
143142
- `state`, the navigation state for the child screen (a shortcut for `navigation.state`)
144143
- `key`, the key of the route (a shortcut for `navigation.state.key`)
145-
146-
147-
### `createNavigationContainer`
148-
149-
If you want your navigator to be usable as a top-level component, (without a navigation prop being passed in), you can use `createNavigationContainer`. This utility will make your navigator act like a top-level navigator when the navigation prop is missing. It will manage the app state, and integrate with app-level nav features, like handling incoming and outgoing links, and Android back button behavior.

docs/hello-react-navigation.md

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ In a web browser, you can link to different pages using an anchor (`<a>`) tag. W
88

99
React Navigation's stack navigator provides a way for your app to transition between screens and manage navigation history. If your app uses only one stack navigator then it is conceptually similar to how a web browser handles navigation state - your app pushes and pops items from the navigation stack as users interact with it, and this results in the user seeing different screens. A key difference between how this works in a web browser and in React Navigation is that React Navigation's stack navigator provides the gestures and animations that you would expect on Android and iOS when navigating between routes in the stack.
1010

11-
All we need to get started using React Navigation is a function called `createStackNavigator`.
11+
Lets start by demonstrating the most common navigator, `createStackNavigator`.
1212

1313
## Creating a stack navigator
1414

@@ -17,47 +17,45 @@ All we need to get started using React Navigation is a function called `createSt
1717
```javascript
1818
// In App.js in a new project
1919

20-
import React from 'react';
21-
import { View, Text } from 'react-native';
22-
import { createStackNavigator } from 'react-navigation';
20+
import React from "react";
21+
import { View, Text } from "react-native";
22+
import { createStackNavigator, createAppContainer } from "react-navigation";
2323

2424
class HomeScreen extends React.Component {
2525
render() {
2626
return (
27-
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
27+
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
2828
<Text>Home Screen</Text>
2929
</View>
3030
);
3131
}
3232
}
3333

34-
export default createStackNavigator({
34+
const AppNavigator = createStackNavigator({
3535
Home: {
3636
screen: HomeScreen
37-
},
37+
}
3838
});
39+
40+
export default createAppContainer(AppNavigator);
3941
```
4042

41-
<a href="https://snack.expo.io/@react-navigation/hello-world-v2" target="blank" class="run-code-button">&rarr; Run this code</a>
43+
<a href="https://snack.expo.io/@react-navigation/hello-world-v2" target="blank" class="run-code-button">&rarr; Run this code</a>
4244

4345
If you run this code, you will see a screen with an empty navigation bar and a grey content area containing your `HomeScreen` component. 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.
4446

4547
> 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.
4648
4749
> The only required configuration for a route is the `screen` component. You can read more about the other options available in the [StackNavigator reference](stack-navigator.html).
4850
49-
In React Native, the component exported from `App.js` is the entry point (or root component) for your app -- it is the component from which every other component descends. It's often useful to have more control over the component at the root of your app than you would get from exporting the result of `createStackNavigator`, so let's export a component that just renders our `RootStack` stack navigator.
51+
In React Native, the component exported from `App.js` is the entry point (or root component) for your app -- it is the component from which every other component descends. It's often useful to have more control over the component at the root of your app than you would get from exporting the result of `createAppContainer`, so let's export a component that just renders our `AppNavigator` stack navigator.
5052

5153
```js
52-
const RootStack = createStackNavigator({
53-
Home: {
54-
screen: HomeScreen
55-
},
56-
});
54+
const AppContainer = createAppContainer(AppNavigator);
5755

5856
export default class App extends React.Component {
5957
render() {
60-
return <RootStack />;
58+
return <AppContainer />;
6159
}
6260
}
6361
```
@@ -82,7 +80,7 @@ The `<RootStack />` component doesn't accept any props -- all configuration is s
8280
class DetailsScreen extends React.Component {
8381
render() {
8482
return (
85-
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
83+
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
8684
<Text>Details Screen</Text>
8785
</View>
8886
);
@@ -92,10 +90,10 @@ class DetailsScreen extends React.Component {
9290
const RootStack = createStackNavigator(
9391
{
9492
Home: HomeScreen,
95-
Details: DetailsScreen,
93+
Details: DetailsScreen
9694
},
9795
{
98-
initialRouteName: 'Home',
96+
initialRouteName: "Home"
9997
}
10098
);
10199

@@ -106,8 +104,8 @@ Now our stack has two _routes_, a `Home` route and a `Details` route. The `Home`
106104

107105
## Summary
108106

109-
* 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.
110-
* `createStackNavigator` is a function that takes a route configuration object and an options object and returns a React component.
111-
* The keys in the route configuration object are the route names and the values are the configuration for that route. The only required property on the configuration is the `screen` (the component to use for the route).
112-
* To specify what the initial route in a stack is, provide an `initialRouteName` on the stack options object.
113-
* [Full source of what we have built so far](https://snack.expo.io/@react-navigation/hello-react-navigation-v2).
107+
- 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.
108+
- `createStackNavigator` is a function that takes a route configuration object and an options object and returns a React component.
109+
- The keys in the route configuration object are the route names and the values are the configuration for that route. The only required property on the configuration is the `screen` (the component to use for the route).
110+
- To specify what the initial route in a stack is, provide an `initialRouteName` on the stack options object.
111+
- [Full source of what we have built so far](https://snack.expo.io/@react-navigation/hello-react-navigation-v2).

docs/web-support.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
id: web-support
3+
title: React Navigation on the Web
4+
sidebar_label: Web Support
5+
---
6+
7+
Starting in v3, React Navigation has built-in support for web sites, including server rendering.
8+
9+
To set up a navigator in a React app, [(such as one created with create-react-app)](https://github.com/react-navigation/example-web):
10+
11+
```js
12+
import { createSwitchNavigator } from "@react-navigation/core";
13+
import { createBrowserApp } from "@react-navigation/web";
14+
15+
const MyNavigator = createSwitchNavigator(routes);
16+
17+
const App = createBrowserApp(MyNavigator);
18+
19+
// now you can render "App" normally
20+
```
21+
22+
## Web Links
23+
24+
We ship a utility out of the box which automatically sets up an `<a>` tag for you with the correct `href`.
25+
26+
This is necessary to properly support server rendering, critical for accessibility, and nice to provide a good user experience when the browser displays what URL the link will go to.
27+
28+
When the app is running, the default browser behavior will be blocked and a navigation action will be dispatched instead.
29+
30+
To render a link to the "Profile" route:
31+
32+
```js
33+
<Link toRoute="Profile" params={{ name: "jamie" }}>
34+
Jamie's Profile
35+
</Link>
36+
```
37+
38+
Depending on the `path` that is set up for the `Profile` route, the above link may render to html as `<a href="/people/jamie">Jamie's Profile</a>`
39+
40+
You can alternatively provide an `action` prop to the `Link`, to specify the exact navigation action that will be used to handle this link.
41+
42+
## Server rendering
43+
44+
You can use the `handleServerRequest` function to get the top-level navigation prop for your app, as well as the current title for this route.
45+
46+
```js
47+
expressApp.get("/*", (req, res) => {
48+
const { navigation, title, options } = handleServerRequest(
49+
AppNavigator.router,
50+
path,
51+
query
52+
);
53+
54+
const markup = renderToString(<AppNavigator navigation={navigation} />);
55+
56+
res.send(
57+
`<!doctype html>
58+
<html lang="">
59+
<head>
60+
<title>${title}</title>
61+
<script src="main.js"></script>
62+
</head>
63+
<body>
64+
<div id="root">${markup}</div>
65+
</body>
66+
</html>`
67+
);
68+
});
69+
```
70+
71+
For a full example, [see a full server+client React web app here](https://github.com/react-navigation/web-server-example)
72+
73+
## Custom navigators for the web
74+
75+
The built-in navigator components such as Stack are often not well suited for web sites, so you may want to create custom navigators yourself.
76+
77+
Your view can use `props.descriptors` to see the current set of screens, get their navigation object, and see the current navigation options. You should use `SceneView` to present your child screen components.
78+
79+
See ["Custom Navigators"](custom-navigators.html) for more details.
80+
81+
For an example of this, see how the custom `SidebarView` and `AppView` are used from [`App.js` in the web server example](https://github.com/react-navigation/web-server-example/blob/master/src/App.js).

website/i18n/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"anti-pitch": "anti-pitch",
1010
"api-reference": "API Reference",
1111
"Overview": "Overview",
12+
"app-containers": "App Containers",
13+
"App Containers": "App Containers",
1214
"auth-flow": "Authentication flows",
1315
"Authentication flows": "Authentication flows",
1416
"bottom-tab-navigator": "createBottomTabNavigator",
@@ -119,6 +121,8 @@
119121
"createTabNavigator": "createTabNavigator",
120122
"transitioner": "Transitioner",
121123
"Transitioner": "Transitioner",
124+
"web-support": "React Navigation on the Web",
125+
"Web Support": "Web Support",
122126
"with-navigation-focus": "withNavigationFocus",
123127
"withNavigationFocus": "withNavigationFocus",
124128
"with-navigation": "withNavigation",

website/sidebars.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"params",
99
"headers",
1010
"header-buttons",
11+
"app-containers",
1112
"modal",
1213
"next-steps",
1314
"glossary-of-terms",
@@ -28,7 +29,8 @@
2829
"deep-linking",
2930
"screen-tracking",
3031
"state-persistence",
31-
"redux-integration"
32+
"redux-integration",
33+
"web-support"
3234
],
3335
"Build your own Navigator": [
3436
"custom-navigator-overview",

0 commit comments

Comments
 (0)