Skip to content

Commit 2958c86

Browse files
committed
Document with and getComponent methods
1 parent af614b6 commit 2958c86

File tree

4 files changed

+104
-58
lines changed

4 files changed

+104
-58
lines changed

versioned_docs/version-7.x/combine-static-with-dynamic.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,10 @@ const FeedTabs = createBottomTabNavigator({
162162
});
163163
```
164164

165-
To use the `FeedTabs` navigator for the `Feed` screen, we need to use the `createComponentForStaticNavigation` function:
165+
To use the `FeedTabs` navigator for the `Feed` screen, we need to get a component from the static navigator using the `getComponent()` method:
166166

167167
```js
168-
import { createComponentForStaticNavigation } from '@react-navigation/native';
169-
170-
// highlight-next-line
171-
const FeedScreen = createComponentForStaticNavigation(FeedTabs, 'Feed');
168+
const FeedScreen = FeedTabs.getComponent();
172169
```
173170

174171
In addition, we can generate the TypeScript types for the `FeedTabs` navigator and use it in the types of `RootStack` without needing to write them manually:

versioned_docs/version-7.x/static-configuration.md

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,48 @@ The above example will only render the `HomeScreen` if the user is logged in.
191191

192192
For more details, see [Authentication flow](auth-flow.md?config=static).
193193

194+
### Passing dynamic props
195+
196+
To pass dynamic props to the navigator created using `createXNavigator`, you can pass a component to the `with` method:
197+
198+
```js
199+
const Drawer = createDrawerNavigator({
200+
screenOptions: {
201+
headerTintColor: 'white',
202+
headerStyle: {
203+
backgroundColor: 'tomato',
204+
},
205+
},
206+
screens: {
207+
Home: HomeScreen,
208+
},
209+
}).with(({ Navigator }) => {
210+
const isLargeScreen = useIsLargeScreen();
211+
212+
return (
213+
<Navigator
214+
screenOptions={{
215+
drawerType: isLargeScreen ? 'permanent' : 'front',
216+
}}
217+
/>
218+
);
219+
});
220+
```
221+
222+
The component passed to `with` receives the `Navigator` component to render as a prop. It accepts any props supported by the navigator.
223+
224+
The `screenOptions` and `screenListeners` props passed to the component will be shallow merged with the ones defined in the static config if any.
225+
226+
### Creating a component
227+
228+
The `getComponent()` method on the object returned by `createXNavigator` functions returns a React component to render for the navigator:
229+
230+
```js
231+
const RootStackNavigator = RootStack.getComponent();
232+
```
233+
234+
This is useful when [combining static and dynamic APIs](combine-static-with-dynamic.md), and not intended to be used directly if you're using static configuration for your whole app.
235+
194236
## `createStaticNavigation`
195237

196238
The `createStaticNavigation` function takes the static config returned by `createXNavigator` functions and returns a React component to render:
@@ -228,21 +270,6 @@ Similar to `NavigationContainer`, the component returned by `createStaticNavigat
228270

229271
See [How does automatic path generation work](configuring-links.md#how-does-automatic-path-generation-work) for more details.
230272

231-
## `createComponentForStaticNavigation`
232-
233-
The `createComponentForStaticNavigation` function takes the static config returned by `createXNavigator` functions and returns a React component to render. The second argument is a name for the component that'd be used in React DevTools:
234-
235-
```js
236-
const RootStackNavigator = createComponentForStaticNavigation(
237-
RootStack,
238-
'RootNavigator'
239-
);
240-
```
241-
242-
The returned component doesn't take any props. All of the configuration is inferred from the static config. It's essentially the same as defining a component using the dynamic API.
243-
244-
This looks similar to `createStaticNavigation` however they are very different. When using static configuration, you'd never use this function directly. The only time you'd use this is if you're migrating away from static configuration and want to reuse existing code you wrote instead of rewriting it to the dynamic API. See [Combining static and dynamic APIs](combine-static-with-dynamic.md) for more details.
245-
246273
## `createPathConfigForStaticNavigation`
247274

248275
The `createPathConfigForStaticNavigation` function takes the static config returned by `createXNavigator` functions and returns a path config object that can be used within the linking config.
@@ -257,4 +284,4 @@ const config = {
257284
};
258285
```
259286

260-
Similar to `createComponentForStaticNavigation`, this is intended to be used when migrating away from static configuration. See [Combining static and dynamic APIs](combine-static-with-dynamic.md) for more details.
287+
This is intended to be used when [combining static and dynamic APIs](combine-static-with-dynamic.md).

versioned_docs/version-8.x/combine-static-with-dynamic.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,10 @@ const FeedTabs = createBottomTabNavigator({
162162
});
163163
```
164164

165-
To use the `FeedTabs` navigator for the `Feed` screen, we need to use the `createComponentForStaticNavigation` function:
165+
To use the `FeedTabs` navigator for the `Feed` screen, we need to get a component from the static navigator using the `getComponent()` method:
166166

167167
```js
168-
import { createComponentForStaticNavigation } from '@react-navigation/native';
169-
170-
// highlight-next-line
171-
const FeedScreen = createComponentForStaticNavigation(FeedTabs, 'Feed');
168+
const FeedScreen = FeedTabs.getComponent();
172169
```
173170

174171
In addition, we can generate the TypeScript types for the `FeedTabs` navigator and use it in the types of `RootStack` without needing to write them manually:

versioned_docs/version-8.x/static-configuration.md

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,63 @@ The above example will only render the `HomeScreen` if the user is logged in.
213213

214214
For more details, see [Authentication flow](auth-flow.md?config=static).
215215

216-
## `createStaticNavigation`
216+
### Passing dynamic props
217+
218+
To pass dynamic props to the navigator created using `createXNavigator`, you can pass a component to the `with` method:
219+
220+
```js
221+
const Drawer = createDrawerNavigator({
222+
screenOptions: {
223+
headerTintColor: 'white',
224+
headerStyle: {
225+
backgroundColor: 'tomato',
226+
},
227+
},
228+
screens: {
229+
Home: HomeScreen,
230+
},
231+
}).with(({ Navigator }) => {
232+
const isLargeScreen = useIsLargeScreen();
233+
234+
return (
235+
<Navigator
236+
screenOptions={{
237+
drawerType: isLargeScreen ? 'permanent' : 'front',
238+
}}
239+
/>
240+
);
241+
});
242+
```
243+
244+
The component passed to `with` receives the `Navigator` component to render as a prop. It accepts any props supported by the navigator.
245+
246+
The `screenOptions` and `screenListeners` props passed to the component will be shallow merged with the ones defined in the static config if any.
247+
248+
### Creating a component
249+
250+
The `getComponent()` method on the object returned by `createXNavigator` functions returns a React component to render for the navigator:
251+
252+
```js
253+
const RootStackNavigator = RootStack.getComponent();
254+
```
255+
256+
This is useful when [combining static and dynamic APIs](combine-static-with-dynamic.md), and not intended to be used directly if you're using static configuration for your whole app.
257+
258+
## `createPathConfigForStaticNavigation`
259+
260+
The `createPathConfigForStaticNavigation` function takes the static config returned by `createXNavigator` functions and returns a path config object that can be used within the linking config.
261+
262+
```js
263+
const config = {
264+
screens: {
265+
Home: {
266+
screens: createPathConfigForStaticNavigation(HomeTabs),
267+
},
268+
},
269+
};
270+
```
271+
272+
This is intended to be used when [combining static and dynamic APIs](combine-static-with-dynamic.md) for more details.
217273

218274
The `createStaticNavigation` function takes the static config returned by `createXNavigator` functions and returns a React component to render:
219275

@@ -251,34 +307,3 @@ Similar to `NavigationContainer`, the component returned by `createStaticNavigat
251307
See [How does automatic path generation work](configuring-links.md#how-does-automatic-path-generation-work) for more details.
252308

253309
By default, linking is enabled in static configuration with automatic path generation. It needs to be explicitly disabled by passing `enabled: false` to the `linking` prop if you don't want linking support.
254-
255-
## `createComponentForStaticNavigation`
256-
257-
The `createComponentForStaticNavigation` function takes the static config returned by `createXNavigator` functions and returns a React component to render. The second argument is a name for the component that'd be used in React DevTools:
258-
259-
```js
260-
const RootStackNavigator = createComponentForStaticNavigation(
261-
RootStack,
262-
'RootNavigator'
263-
);
264-
```
265-
266-
The returned component doesn't take any props. All of the configuration is inferred from the static config. It's essentially the same as defining a component using the dynamic API.
267-
268-
This looks similar to `createStaticNavigation` however they are very different. When using static configuration, you'd never use this function directly. The only time you'd use this is if you're migrating away from static configuration and want to reuse existing code you wrote instead of rewriting it to the dynamic API. See [Combining static and dynamic APIs](combine-static-with-dynamic.md) for more details.
269-
270-
## `createPathConfigForStaticNavigation`
271-
272-
The `createPathConfigForStaticNavigation` function takes the static config returned by `createXNavigator` functions and returns a path config object that can be used within the linking config.
273-
274-
```js
275-
const config = {
276-
screens: {
277-
Home: {
278-
screens: createPathConfigForStaticNavigation(HomeTabs),
279-
},
280-
},
281-
};
282-
```
283-
284-
Similar to `createComponentForStaticNavigation`, this is intended to be used when migrating away from static configuration. See [Combining static and dynamic APIs](combine-static-with-dynamic.md) for more details.

0 commit comments

Comments
 (0)