Skip to content

Commit

Permalink
Rework fundamentals to accomodate static API (#1293)
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 authored Dec 25, 2023
1 parent 4806039 commit 9d13c4d
Show file tree
Hide file tree
Showing 43 changed files with 964 additions and 471 deletions.
3 changes: 0 additions & 3 deletions sidebars.js

This file was deleted.

10 changes: 7 additions & 3 deletions static/examples/7.x/go-back.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

function HomeScreen({ navigation }) {
function HomeScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
Expand All @@ -15,7 +17,9 @@ function HomeScreen({ navigation }) {
);
}

function DetailsScreen({ navigation }) {
function DetailsScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
Expand Down
10 changes: 7 additions & 3 deletions static/examples/7.x/multiple-navigate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

function HomeScreen({ navigation }) {
function HomeScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
Expand All @@ -15,7 +17,9 @@ function HomeScreen({ navigation }) {
);
}

function DetailsScreen({ navigation }) {
function DetailsScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
Expand Down
10 changes: 7 additions & 3 deletions static/examples/7.x/multiple-push.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

function HomeScreen({ navigation }) {
function HomeScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
Expand All @@ -15,7 +17,9 @@ function HomeScreen({ navigation }) {
);
}

function DetailsScreen({ navigation }) {
function DetailsScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
Expand Down
6 changes: 4 additions & 2 deletions static/examples/7.x/new-screen.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

function HomeScreen({ navigation }) {
function HomeScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
Expand Down
14 changes: 10 additions & 4 deletions static/examples/7.x/params-nested-navigators.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createDrawerNavigator } from '@react-navigation/drawer';

function SettingsScreen({ route, navigation }) {
function SettingsScreen({ route }) {
const navigation = useNavigation();

const { user } = route.params;
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
Expand All @@ -18,15 +20,19 @@ function SettingsScreen({ route, navigation }) {
);
}

function ProfileScreen({ navigation }) {
function ProfileScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Profile Screen</Text>
</View>
);
}

function HomeScreen({ navigation }) {
function HomeScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
Expand Down
9 changes: 6 additions & 3 deletions static/examples/7.x/passing-params-back.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from 'react';
import { Text, TextInput, View, Button } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

function HomeScreen({ navigation, route }) {
function HomeScreen({ route }) {
const navigation = useNavigation();

React.useEffect(() => {
if (route.params?.post) {
// Post updated, do something with `route.params.post`
Expand All @@ -22,7 +24,8 @@ function HomeScreen({ navigation, route }) {
);
}

function CreatePostScreen({ navigation, route }) {
function CreatePostScreen({ route }) {
const navigation = useNavigation();
const [postText, setPostText] = React.useState('');

return (
Expand Down
10 changes: 7 additions & 3 deletions static/examples/7.x/passing-params.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from 'react';
import { Text, View, Button } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

function HomeScreen({ navigation }) {
function HomeScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
Expand All @@ -21,7 +23,9 @@ function HomeScreen({ navigation }) {
);
}

function DetailsScreen({ route, navigation }) {
function DetailsScreen({ route }) {
const navigation = useNavigation();

/* 2. Get the param */
const { itemId } = route.params;
const { otherParam } = route.params;
Expand Down
10 changes: 7 additions & 3 deletions static/examples/7.x/pop-to-top.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from 'react';
import { Button, View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

function HomeScreen({ navigation }) {
function HomeScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
Expand All @@ -15,7 +17,9 @@ function HomeScreen({ navigation }) {
);
}

function DetailsScreen({ navigation }) {
function DetailsScreen() {
const navigation = useNavigation();

return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Details Screen</Text>
Expand Down
4 changes: 2 additions & 2 deletions versioned_docs/version-7.x/bottom-tab-navigator.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The `Tab.Navigator` component accepts following props:

#### `id`

Optional unique ID for the navigator. This can be used with [`navigation.getParent`](navigation-prop.md#getparent) to refer to this navigator in a child navigator.
Optional unique ID for the navigator. This can be used with [`navigation.getParent`](navigation-object.md#getparent) to refer to this navigator in a child navigator.

#### `initialRouteName`

Expand Down Expand Up @@ -442,7 +442,7 @@ React.useEffect(() => {

### Helpers

The tab navigator adds the following methods to the navigation prop:
The tab navigator adds the following methods to the navigation object:

#### `jumpTo`

Expand Down
48 changes: 0 additions & 48 deletions versioned_docs/version-7.x/connecting-navigation-prop.md

This file was deleted.

2 changes: 1 addition & 1 deletion versioned_docs/version-7.x/custom-navigators.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The hook returns an object with following properties:
- `navigation` - The navigation object containing various helper methods for the navigator to manipulate the [navigation state](navigation-state.md). This isn't the same as the navigation object for the screen and includes some helpers such as `emit` to emit events to the screens.
- `descriptors` - This is an object containing descriptors for each route with the route keys as its properties. The descriptor for a route can be accessed by `descriptors[route.key]`. Each descriptor contains the following properties:

- `navigation` - The navigation prop for the screen. You don't need to pass this to the screen manually. But it's useful if we're rendering components outside the screen that need to receive `navigation` prop as well, such as a header component.
- `navigation` - The navigation object for the screen. You don't need to pass this to the screen manually. But it's useful if we're rendering components outside the screen that need to receive `navigation` prop as well, such as a header component.
- `options` - A getter which returns the options such as `title` for the screen if they are specified.
- `render` - A function which can be used to render the actual screen. Calling `descriptors[route.key].render()` will return a React element containing the screen content. It's important to use this method to render a screen, otherwise any child navigators won't be connected to the navigation tree properly.

Expand Down
2 changes: 1 addition & 1 deletion versioned_docs/version-7.x/custom-routers.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ const MyTabRouter = options => {
};
```

Instead of writing a custom router to handle custom actions, you can [pass a function to `dispatch`](navigation-prop.md#dispatch) instead. It's cleaner and recommended instead of overriding routers.
Instead of writing a custom router to handle custom actions, you can [pass a function to `dispatch`](navigation-object.md#dispatch) instead. It's cleaner and recommended instead of overriding routers.

### Blocking Navigation Actions

Expand Down
4 changes: 2 additions & 2 deletions versioned_docs/version-7.x/drawer-navigator.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ The `Drawer.Navigator` component accepts following props:

#### `id`

Optional unique ID for the navigator. This can be used with [`navigation.getParent`](navigation-prop.md#getparent) to refer to this navigator in a child navigator.
Optional unique ID for the navigator. This can be used with [`navigation.getParent`](navigation-object.md#getparent) to refer to this navigator in a child navigator.

#### `initialRouteName`

Expand Down Expand Up @@ -529,7 +529,7 @@ If you have custom drawer content, make sure to emit this event.

### Helpers

The drawer navigator adds the following methods to the navigation prop:
The drawer navigator adds the following methods to the navigation object:

#### `openDrawer`

Expand Down
27 changes: 22 additions & 5 deletions versioned_docs/version-7.x/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ title: Getting started
sidebar_label: Getting started
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

What follows within the _Fundamentals_ section of this documentation is a tour of the most important aspects of React Navigation. It should cover enough for you to know how to build your typical small mobile application, and give you the background that you need to dive deeper into the more advanced parts of React Navigation.

## Pre-requisites
Expand Down Expand Up @@ -114,9 +117,25 @@ When you use a navigator (such as stack navigator), you'll need to follow the in

:::

## Wrapping your app in `NavigationContainer`
## Setting up React Navigation

Once you've installed and configured the dependencies, you can move on to setting up your project to use React Navigation.

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.

There are 2 primary ways to configure the navigators:

### Static configuration

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`:
The static configuration API has reduced boilerplate and simplifies things such as TypeScript types and deep linking. If you're starting a new project or are new to React Navigation, this is the **recommended way** to set up your app. If you need more flexibility in the future, you can always mix and match with the dynamic configuration.

Continue to ["Hello React Navigation"](hello-react-navigation.md?config=static) to start writing some code with the static API.

### Dynamic configuration

The dynamic configuration allows for more flexibility but requires more boilerplate and configuration (e.g. for deep links, typescript etc.).

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`:

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

:::

Now you are ready to build and run your app on the device/simulator.

Continue to ["Hello React Navigation"](hello-react-navigation.md) to start writing some code.
Continue to ["Hello React Navigation"](hello-react-navigation.md?config=dynamic) to start writing some code with the dynamic API.
Loading

0 comments on commit 9d13c4d

Please sign in to comment.