Skip to content

Commit 5ce423a

Browse files
committed
Add docs for 6.x
1 parent 965f949 commit 5ce423a

File tree

87 files changed

+12517
-36
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+12517
-36
lines changed

docusaurus.config.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const path = require('path');
2-
const versions = require('./versions.json');
32

43
module.exports = {
54
title: 'React Navigation',
@@ -147,14 +146,18 @@ module.exports = {
147146
'@docusaurus/preset-classic',
148147
{
149148
docs: {
150-
sidebarPath: require.resolve('./sidebars.js'),
151149
docLayoutComponent: require.resolve('./src/layouts/DocPage.js'),
152150
docItemComponent: require.resolve('./src/layouts/DocItem.js'),
153151
editUrl:
154152
'https://github.com/react-navigation/react-navigation.github.io/edit/main/',
155153
remarkPlugins: [require('./src/plugins/remark-npm2yarn')],
156154
includeCurrentVersion: false,
157155
lastVersion: '5.x',
156+
versions: {
157+
'6.x': {
158+
label: '6.x (next)',
159+
},
160+
},
158161
},
159162
theme: {
160163
customCss: require.resolve('./src/css/custom.css'),

src/pages/versions.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import versions from '../../versions.json';
1111
function Version() {
1212
const context = useDocusaurusContext();
1313
const {siteConfig = {}} = context;
14-
const latestVersion = versions[0];
15-
const pastVersions = versions.filter(version => version !== latestVersion);
14+
const latestVersion = versions[1];
15+
const pastVersions = versions.slice(1, 1);
1616
const repoUrl = `https://github.com/${siteConfig.organizationName}/${siteConfig.projectName}`;
1717

1818
return (

src/theme/DocVersionSuggestions/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function DocVersionSuggestions() {
2929
latestVersionSuggestion,
3030
} = useDocVersionSuggestions(pluginId); // No suggestion to be made
3131

32-
if (!latestVersionSuggestion || activeVersion.name === '4.x') {
32+
if (!latestVersionSuggestion || activeVersion.name === '4.x' || activeVersion.name === '6.x') {
3333
return <></>;
3434
} // try to link to same doc in latest version (not always possible)
3535
// fallback to main doc of latest version

versioned_docs/version-5.x/configuring-links.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ const state = {
444444

445445
It's not possible to pass params to the initial screen through the URL. So make sure that your initial route doesn't need any params or specify `initialParams` to pass required params.
446446

447-
In this case, any params in the URL are only passed to the `Profile` screen which matches the path pattern `users/:id`, and the `Feed` screen doesn't receive any params. If you want to have the same params in the `Feed` screen, you can specify a [custom `getStateFromPath` function](use-linking.md#getstatefrompath) and copy those params.
447+
In this case, any params in the URL are only passed to the `Profile` screen which matches the path pattern `users/:id`, and the `Feed` screen doesn't receive any params. If you want to have the same params in the `Feed` screen, you can specify a [custom `getStateFromPath` function](navigation-container.md#linkinggetstatefrompath) and copy those params.
448448

449449
Similarly, if you want to access params of a parent screen from a child screen, you can use [React Context](https://reactjs.org/docs/context.html) to expose them.
450450

@@ -610,7 +610,7 @@ Depending on your requirements, you can use this functionality to parse and stri
610610

611611
## Advanced cases
612612

613-
For some advanced cases, specifying the mapping may not be sufficient. To handle such cases, you can specify a custom function to parse the URL into a state object ([`getStateFromPath`](use-linking.md#getstatefrompath)), and a custom function to serialize the state object into an URL ([`getPathFromState`](use-linking.md#getpathfromstate)).
613+
For some advanced cases, specifying the mapping may not be sufficient. To handle such cases, you can specify a custom function to parse the URL into a state object ([`getStateFromPath`](navigation-container.md#linkinggetstatefrompath)), and a custom function to serialize the state object into an URL ([`getPathFromState`](navigation-container.md#linkinggetpathfromstate)).
614614

615615
Example:
616616

versioned_docs/version-5.x/material-top-tab-navigator.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This wraps [`react-native-tab-view`](https://github.com/react-native-community/r
1717
To use this navigator, ensure that you have [`@react-navigation/native` and its dependencies (follow this guide)](getting-started.md), then install [`@react-navigation/material-top-tabs`](https://github.com/react-navigation/react-navigation/tree/main/packages/material-top-tabs):
1818

1919
```bash npm2yarn
20-
npm install @react-navigation/material-top-tabs react-native-tab-view
20+
npm install @react-navigation/material-top-tabs react-native-tab-view@^2.16.0
2121
```
2222

2323
## API Definition

versioned_docs/version-5.x/troubleshooting.md

+4-28
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,16 @@ If it's missing these extensions, add them and then clear metro cache as shown i
6464
6565
This might happen if you have an old version of the `metro-react-native-babel-preset` package. Try upgrading it to the latest version.
6666
67-
If you use `npm`:
68-
69-
```sh
67+
```sh npm2yarn
7068
npm install --save-dev metro-react-native-babel-preset
7169
```
7270
73-
If you use `yarn`:
74-
75-
```sh
76-
yarn add --dev metro-react-native-babel-preset
77-
```
78-
7971
If you have `@babel/core` installed, also upgrade it to latest version.
8072
81-
If you use `npm`:
82-
83-
```sh
73+
```sh npm2yarn
8474
npm install --save-dev @babel/core
8575
```
8676
87-
If you use `yarn`:
88-
89-
```sh
90-
yarn add --dev @babel/core
91-
```
92-
9377
If upgrading the packages don't help, you can also try deleting your `node_modules` as well as lock the file and reinstall your dependencies.
9478
9579
If you use `npm`:
@@ -114,18 +98,10 @@ After upgrading or reinstalling the packages, you should also clear Metro bundle
11498
11599
This might happen if you have an old version of TypeScript in your project. You can try upgrading it:
116100
117-
If you use `npm`:
118-
119-
```sh
101+
```sh npm2yarn
120102
npm install --save-dev typescript
121103
```
122104
123-
If you use `yarn`:
124-
125-
```sh
126-
yarn add --dev typescript
127-
```
128-
129105
## I'm getting an error "null is not an object (evaluating 'RNGestureHandlerModule.default.Direction')"
130106
131107
This and some similar errors might occur if you have a bare React Native project and the library [`react-native-gesture-handler`](https://github.com/software-mansion/react-native-gesture-handler) library isn't linked.
@@ -260,7 +236,7 @@ YellowBox.ignoreWarnings([
260236
```
261237
262238
263-
> Note: As of React Native 0.63 YellowBox has been deprecated in favor of LogBox, so the previous
239+
> Note: As of React Native 0.63 YellowBox has been deprecated in favor of LogBox, so the previous
264240
code would be written as follows:
265241
>```js
266242
>import { LogBox } from 'react-native';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
id: MST-integration
3+
title: Integrating with MobX State Tree
4+
sidebar_label: MobX State Tree integration
5+
---
6+
7+
> TODO: This guide is incomplete. Please help improve this by sending pull requests.
8+
9+
This guide explores possible way to use React Navigation in a React Native project that uses [MobX State Tree](https://github.com/mobxjs/mobx-state-tree)(MST) for state management. The guide is accompanied by a [sample app](https://github.com/vonovak/react-navigation-mst-demo). Parts of the guide may be relevant also for users of [MobX](https://github.com/mobxjs/mobx) but please be aware of the fact that MobX does not come with a built-in solution for (de)serializing its state.
10+
11+
> Please note that in this guide, Mobx State Tree is not used to manage the navigation state itself - just the navigation params!
12+
13+
## Overview
14+
15+
Our goal with this guide is to use MST with React Navigation and achieve optimal developer experience. In the scope of this guide, this means allowing us to do a full JS reload and be brought back to the state before the reload happened.
16+
17+
We will do this by persisting the [navigation state](navigation-state.md) using the React Navigation's [built-in mechanism](state-persistence.md). We also need to persist the app state and navigation params - that way, when you're working on a screen in your app and do a full JS reload, you will be brought back to the same screen, with the same data in it.
18+
19+
## Guide
20+
21+
First, start by creating initial navigation structure and React components. When you're done with that, continue with modelling your state in MST. If you want to learn more about this, check out the [egghead.io course](https://egghead.io/lessons/react-describe-your-application-domain-using-mobx-state-tree-mst-models).
22+
23+
At this point, you're probably wondering how to connect your MST objects with the components. The answer is in the [mobx-react package](https://github.com/mobxjs/mobx-react) that contains React bindings for MobX (they also work for MST). You will likely be using the `Provider` component and the `inject` and `observer` functions.
24+
25+
Use `Provider` to wrap what you return from your root component's render method:
26+
27+
```js
28+
<Provider myObject={this.myObject}>
29+
<NavigationContainer>{/* Screen configuration */}</NavigationContainer>
30+
</Provider>
31+
```
32+
33+
this will allow you to access `myObject` from any React component in the application through the `inject` function which can be quite useful.
34+
35+
Use `observer` function to wrap all components that render observable data. This will make sure the components re-render once the data they render changes.
36+
37+
### Navigation params
38+
39+
Screens in your application often depend on params. React Navigation allows you to [send params](params.md) from one screen to another. These params are stored in the navigation state. However, in order to persist the navigation state, it needs to be serializable. This requirement does not play well with MST, because the MST objects are complex objects and React Navigation doesn't know how to (de)serialize them. In this guide, we will work around this by storing the navigation params ourselves.
40+
41+
This means that rather than sending the params from one screen to another (eg. with `props.navigation.navigate('MyScreen', { complexMSTObject })`) we will store the params to a navigation store, then navigate without sending any params, and on the target screen, we'll pick the params up from the navigation store.
42+
43+
To give an example, the navigation store may look similar to this:
44+
45+
```js
46+
import { types, onSnapshot, getRoot } from 'mobx-state-tree';
47+
import { Product } from '../models/Product';
48+
import { User } from '../models/User';
49+
50+
export const NavigationStore = types
51+
.model('NavigationStore', {
52+
productDetailScreenParams: types.map(
53+
types.model('ProductDetailScreenParams', {
54+
product: types.optional(types.safeReference(Product)),
55+
})
56+
),
57+
userProfileScreenParams: types.model('UserProfileScreenParams', {
58+
user: types.maybe(types.safeReference(User)),
59+
}),
60+
})
61+
.actions(self => ({
62+
...
63+
}));
64+
```
65+
66+
Note that `userProfileScreenParams` is a simple model with a `user` entry, while `productDetailScreenParams` is a map of `ProductDetailScreenParams` model. The reason we chose this shape of data is that we only have a single user profile screen in our app which reads its params from `userProfileScreenParams`. `productDetailScreenParams` is a map because the app can have several product screens on a stack. Each screen points to a `Product` instance saved in the map. The keys into the map are the React Navigation [keys](navigation-state.md): think of the `key` as of an identifier of the route.
67+
68+
Your navigation store may also be just one map where for each screen (regardless if it is a product or user profile screen), we store its navigation params. This is the approach taken in the [sample app](https://github.com/vonovak/react-navigation-mst-demo).
69+
70+
## Summary
71+
72+
- you can use React Navigation with MobX State Tree in a React Native app
73+
- use the `Provider` component and the `inject` and `observer` functions to wire up MobX or MST with React
74+
- it's possible to persist the entire application state and restore it upon JS reload
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
id: alternatives
3+
title: Alternative libraries
4+
sidebar_label: Alternative libraries
5+
---
6+
7+
React Navigation isn't your only option for routing and navigation in React Native. If the [pitch & anti-pitch](pitch.md) or the API design leave you wanting to explore other options, here are a couple to consider.
8+
9+
- [react-native-router-flux](https://github.com/aksonov/react-native-router-flux): this library is based on React Navigation but provides you with a different API to interact with it.
10+
- [react-native-navigation](https://github.com/wix/react-native-navigation): uses the underlying native APIs on iOS and Android, similar to [createNativeStackNavigator](native-stack-navigator.md). This is a popular alternative to React Navigation and may be a better fit for you if you are trying to integrate React Native into an existing large native app.

0 commit comments

Comments
 (0)