Skip to content

Commit 1975c20

Browse files
committed
Document passing additional data with static config
1 parent cd42f2d commit 1975c20

4 files changed

Lines changed: 121 additions & 11 deletions

File tree

versioned_docs/version-7.x/hello-react-navigation.md

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Now our stack has two _routes_, a `Home` route and a `Details` route. A route ca
245245

246246
:::warning
247247

248-
When using the dynamic API, the `component` prop accepts a component, not a render function. Don't pass an inline function (e.g. `component={() => <HomeScreen />}`), or your component will unmount and remount losing all state when the parent component re-renders. See [Passing additional props](#passing-additional-props) for alternatives.
248+
When using the dynamic API, the `component` prop accepts a component, not a render function. Don't pass an inline function (e.g. `component={() => <HomeScreen />}`), or your component will unmount and remount losing all state when the parent component re-renders. See [Passing additional data](#passing-additional-data) for alternatives.
249249

250250
:::
251251

@@ -493,19 +493,72 @@ export default function App() {
493493
</TabItem>
494494
</Tabs>
495495

496-
## Passing additional props
496+
## Passing additional data
497497

498498
<Tabs groupId="config" queryString="config">
499499
<TabItem value="static" label="Static" default>
500500

501-
Passing additional props to a screen is not supported in the static API.
501+
To pass additional data to a screen, use `.with` to wrap the navigator with [React context](https://react.dev/reference/react/useContext) to pass data to the screens:
502+
503+
```js
504+
// highlight-next-line
505+
const ExtraDataContext = React.createContext();
506+
507+
function HomeScreen() {
508+
// highlight-next-line
509+
const extraData = React.useContext(ExtraDataContext);
510+
511+
return <Text>{extraData}</Text>;
512+
}
513+
514+
const RootStack = createNativeStackNavigator({
515+
screens: {
516+
Home: createNativeStackScreen({
517+
screen: HomeScreen,
518+
}),
519+
},
520+
}).with(({ Navigator }) => {
521+
return (
522+
// highlight-next-line
523+
<ExtraDataContext.Provider value={someData}>
524+
<Navigator />
525+
</ExtraDataContext.Provider>
526+
);
527+
});
528+
```
502529

503530
</TabItem>
504531
<TabItem value="dynamic" label="Dynamic">
505532

506-
We can pass additional props to a screen with 2 approaches:
533+
We can pass additional data to a screen with 2 approaches:
534+
535+
1. [React context](https://react.dev/reference/react/useContext) and wrap the navigator with a context provider to pass data to the screens (recommended):
536+
537+
```js
538+
// highlight-next-line
539+
const ExtraDataContext = React.createContext();
540+
541+
function HomeScreen() {
542+
// highlight-next-line
543+
const extraData = React.useContext(ExtraDataContext);
544+
545+
return <Text>{extraData}</Text>;
546+
}
547+
548+
const Stack = createNativeStackNavigator();
549+
550+
function RootStack() {
551+
return (
552+
// highlight-next-line
553+
<ExtraDataContext.Provider value={someData}>
554+
<Stack.Navigator>
555+
<Stack.Screen name="Home" component={HomeScreen} />
556+
</Stack.Navigator>
557+
</ExtraDataContext.Provider>
558+
);
559+
}
560+
```
507561

508-
1. [React context](https://react.dev/reference/react/useContext) and wrap the navigator with a context provider to pass data to the screens (recommended).
509562
2. Render callback for the screen instead of specifying a `component` prop:
510563

511564
```js
@@ -545,6 +598,7 @@ If you are using TypeScript, you will need to specify the types accordingly. You
545598
- Each property under screens refers to the name of the route, and the value is the component to render for the route.
546599
- To specify what the initial route in a stack is, provide an [`initialRouteName`](navigator.md#initial-route-name) option for the navigator.
547600
- To specify screen-specific options, we can specify an [`options`](screen-options.md#options-prop-on-screen) property, and for common options, we can specify [`screenOptions`](screen-options.md#screenoptions-prop-on-the-navigator).
601+
- To pass additional data to a screen, we can use React context and use `.with` to wrap the navigator with a context provider to pass data to the screens.
548602

549603
</TabItem>
550604
<TabItem value="dynamic" label="Dynamic">
@@ -554,6 +608,7 @@ If you are using TypeScript, you will need to specify the types accordingly. You
554608
- Each [`Stack.Screen`](screen.md) component takes a [`name`](screen.md#name) prop which refers to the name of the route and [`component`](screen.md#component) prop which specifies the component to render for the route. These are the 2 required props.
555609
- To specify what the initial route in a stack is, provide an [`initialRouteName`](navigator.md#initial-route-name) as the prop for the navigator.
556610
- To specify screen-specific options, we can pass an [`options`](screen-options.md#options-prop-on-screen) prop to `Stack.Screen`, and for common options, we can pass [`screenOptions`](screen-options.md#screenoptions-prop-on-the-navigator) to `Stack.Navigator`.
611+
- To pass additional data to a screen, we can either use React context and wrap the navigator with a context provider to pass data to the screens (recommended), or we can use a render callback for the screen instead of specifying a `component` prop.
557612

558613
</TabItem>
559614
</Tabs>

versioned_docs/version-7.x/troubleshooting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Example of some use cases for passing functions in params are the following:
259259
- To pass a callback to use in a header button. This can be achieved using `navigation.setOptions` instead. See the [guide for header buttons](header-buttons.md#header-interaction-with-its-screen-component) for examples.
260260
- To pass a callback to the next screen which it can call to pass some data back. You can usually achieve it using `popTo` instead. See [passing params to a previous screen](params.md#passing-params-to-a-previous-screen) for examples.
261261
- To pass complex data to another screen. Instead of passing the data `params`, you can store that complex data somewhere else (like a global store), and pass an id instead. Then the screen can get the data from the global store using the id. See [what should be in params](params.md#what-should-be-in-params).
262-
- Pass data, callbacks etc. from a parent to child screens. You can either use React Context, or pass a children callback to pass these down instead of using params. See [passing additional props](hello-react-navigation.md#passing-additional-props).
262+
- Pass data, callbacks etc. from a parent to child screens. You can either use React Context, or pass a children callback to pass these down instead of using params. See [passing additional data](hello-react-navigation.md#passing-additional-data).
263263
264264
We don't generally recommend passing functions in params. But if you don't use state persistence, deep links, or use React Navigation on Web, then you can choose to ignore it. To ignore the warning, you can use `LogBox.ignoreLogs`.
265265

versioned_docs/version-8.x/hello-react-navigation.md

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,19 +255,72 @@ export default function App() {
255255
}
256256
```
257257

258-
## Passing additional props
258+
## Passing additional data
259259

260260
<Tabs groupId="config" queryString="config">
261261
<TabItem value="static" label="Static" default>
262262

263-
Passing additional props to a screen is not supported in the static API.
263+
To pass additional data to a screen, use `.with` to wrap the navigator with [React context](https://react.dev/reference/react/useContext) to pass data to the screens:
264+
265+
```js
266+
// highlight-next-line
267+
const ExtraDataContext = React.createContext();
268+
269+
function HomeScreen() {
270+
// highlight-next-line
271+
const extraData = React.useContext(ExtraDataContext);
272+
273+
return <Text>{extraData}</Text>;
274+
}
275+
276+
const RootStack = createNativeStackNavigator({
277+
screens: {
278+
Home: createNativeStackScreen({
279+
screen: HomeScreen,
280+
}),
281+
},
282+
}).with(({ Navigator }) => {
283+
return (
284+
// highlight-next-line
285+
<ExtraDataContext.Provider value={someData}>
286+
<Navigator />
287+
</ExtraDataContext.Provider>
288+
);
289+
});
290+
```
264291

265292
</TabItem>
266293
<TabItem value="dynamic" label="Dynamic">
267294

268-
We can pass additional props to a screen with 2 approaches:
295+
We can pass additional data to a screen with 2 approaches:
296+
297+
1. [React context](https://react.dev/reference/react/useContext) and wrap the navigator with a context provider to pass data to the screens (recommended):
298+
299+
```js
300+
// highlight-next-line
301+
const ExtraDataContext = React.createContext();
302+
303+
function HomeScreen() {
304+
// highlight-next-line
305+
const extraData = React.useContext(ExtraDataContext);
306+
307+
return <Text>{extraData}</Text>;
308+
}
309+
310+
const Stack = createNativeStackNavigator();
311+
312+
function RootStack() {
313+
return (
314+
// highlight-next-line
315+
<ExtraDataContext.Provider value={someData}>
316+
<Stack.Navigator>
317+
<Stack.Screen name="Home" component={HomeScreen} />
318+
</Stack.Navigator>
319+
</ExtraDataContext.Provider>
320+
);
321+
}
322+
```
269323

270-
1. [React context](https://react.dev/reference/react/useContext) and wrap the navigator with a context provider to pass data to the screens (recommended).
271324
2. Render callback for the screen instead of specifying a `component` prop:
272325

273326
```js
@@ -316,6 +369,7 @@ Now that we have two screens, "How do we navigate from `Home` to `Details`?". Th
316369
- Each property under screens refers to the name of the route, and the value is the component to render for the route.
317370
- To specify what the initial route in a stack is, provide an [`initialRouteName`](navigator.md#initial-route-name) option for the navigator.
318371
- To specify screen-specific options, we can specify an [`options`](screen-options.md#options-prop-on-screen) property, and for common options, we can specify [`screenOptions`](screen-options.md#screenoptions-prop-on-the-navigator).
372+
- To pass additional data to a screen, we can use React context and use `.with` to wrap the navigator with a context provider to pass data to the screens.
319373

320374
</TabItem>
321375
<TabItem value="dynamic" label="Dynamic">
@@ -325,6 +379,7 @@ Now that we have two screens, "How do we navigate from `Home` to `Details`?". Th
325379
- Each [`Stack.Screen`](screen.md) component takes a [`name`](screen.md#name) prop which refers to the name of the route and [`component`](screen.md#component) prop which specifies the component to render for the route. These are the 2 required props.
326380
- To specify what the initial route in a stack is, provide an [`initialRouteName`](navigator.md#initial-route-name) as the prop for the navigator.
327381
- To specify screen-specific options, we can pass an [`options`](screen-options.md#options-prop-on-screen) prop to `Stack.Screen`, and for common options, we can pass [`screenOptions`](screen-options.md#screenoptions-prop-on-the-navigator) to `Stack.Navigator`.
382+
- To pass additional data to a screen, we can either use React context and wrap the navigator with a context provider to pass data to the screens (recommended), or we can use a render callback for the screen instead of specifying a `component` prop.
328383

329384
</TabItem>
330385
</Tabs>

versioned_docs/version-8.x/troubleshooting.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ Example of some use cases for passing functions in params are the following:
259259
- To pass a callback to use in a header button. This can be achieved using `navigation.setOptions` instead. See the [guide for header buttons](header-buttons.md#header-interaction-with-its-screen-component) for examples.
260260
- To pass a callback to the next screen which it can call to pass some data back. You can usually achieve it using `popTo` instead. See [passing params to a previous screen](params.md#passing-params-to-a-previous-screen) for examples.
261261
- To pass complex data to another screen. Instead of passing the data `params`, you can store that complex data somewhere else (like a global store), and pass an id instead. Then the screen can get the data from the global store using the id. See [what should be in params](params.md#what-should-be-in-params).
262-
- Pass data, callbacks etc. from a parent to child screens. You can either use React Context, or pass a children callback to pass these down instead of using params. See [passing additional props](hello-react-navigation.md#passing-additional-props).
262+
- Pass data, callbacks etc. from a parent to child screens. You can either use React Context, or pass a children callback to pass these down instead of using params. See [passing additional data](hello-react-navigation.md#passing-additional-data).
263263
264264
We don't generally recommend passing functions in params. But if you don't use state persistence, deep links, or use React Navigation on Web, then you can choose to ignore it. To ignore the warning, you can use `LogBox.ignoreLogs`.
265265

0 commit comments

Comments
 (0)