Skip to content

Commit 41e1046

Browse files
committed
Use static2dynamic for more examples
1 parent 276457f commit 41e1046

37 files changed

+17
-1356
lines changed

versioned_docs/version-8.x/custom-android-back-button-handling.md

Lines changed: 1 addition & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,13 @@ title: Custom Android back button behavior
44
sidebar_label: Android back button behavior
55
---
66

7-
import Tabs from '@theme/Tabs';
8-
import TabItem from '@theme/TabItem';
9-
107
By default, when user presses the Android hardware back button, react-navigation will pop a screen or exit the app if there are no screens to pop. This is a sensible default behavior, but there are situations when you might want to implement custom handling.
118

129
As an example, consider a screen where user is selecting items in a list, and a "selection mode" is active. On a back button press, you would first want the "selection mode" to be deactivated, and the screen should be popped only on the second back button press. The following code snippet demonstrates the situation. We make use of [`BackHandler`](https://reactnative.dev/docs/backhandler.html) which comes with react-native, along with the `useFocusEffect` hook to add our custom `hardwareBackPress` listener.
1310

1411
Returning `true` from `onBackPress` denotes that we have handled the event, and react-navigation's listener will not get called, thus not popping the screen. Returning `false` will cause the event to bubble up and react-navigation's listener will pop the screen.
1512

16-
<Tabs groupId="config" queryString="config">
17-
<TabItem value="static" label="Static" default>
18-
19-
```js name="Custom android back button" snack
13+
```js name="Custom android back button" snack static2dynamic
2014
import * as React from 'react';
2115
import { Text, View, BackHandler, StyleSheet } from 'react-native';
2216
import { createStaticNavigation } from '@react-navigation/native';
@@ -126,125 +120,6 @@ const styles = StyleSheet.create({
126120
});
127121
```
128122

129-
</TabItem>
130-
<TabItem value="dynamic" label="Dynamic">
131-
132-
```js name="Custom android back button" snack
133-
import * as React from 'react';
134-
import { Text, View, BackHandler, StyleSheet } from 'react-native';
135-
import { NavigationContainer } from '@react-navigation/native';
136-
import { useFocusEffect } from '@react-navigation/native';
137-
import { createNativeStackNavigator } from '@react-navigation/native-stack';
138-
import { PlatformPressable, Button } from '@react-navigation/elements';
139-
140-
const Stack = createNativeStackNavigator();
141-
142-
const listData = [{ key: 'Apple' }, { key: 'Orange' }, { key: 'Carrot' }];
143-
144-
// codeblock-focus-start
145-
function ScreenWithCustomBackBehavior() {
146-
// codeblock-focus-end
147-
148-
const [selected, setSelected] = React.useState(listData[0].key);
149-
const [isSelectionModeEnabled, setIsSelectionModeEnabled] =
150-
React.useState(false);
151-
// codeblock-focus-start
152-
// ...
153-
154-
useFocusEffect(
155-
React.useCallback(() => {
156-
const onBackPress = () => {
157-
if (isSelectionModeEnabled) {
158-
setIsSelectionModeEnabled(false);
159-
return true;
160-
} else {
161-
return false;
162-
}
163-
};
164-
165-
const subscription = BackHandler.addEventListener(
166-
'hardwareBackPress',
167-
onBackPress
168-
);
169-
170-
return () => subscription.remove();
171-
}, [isSelectionModeEnabled])
172-
);
173-
// codeblock-focus-end
174-
175-
return (
176-
<View style={styles.container}>
177-
{listData.map((item) => (
178-
<React.Fragment key={item.key}>
179-
{isSelectionModeEnabled ? (
180-
<PlatformPressable
181-
onPress={() => {
182-
setSelected(item.key);
183-
}}
184-
style={{
185-
textDecorationLine: item.key === selected ? 'underline' : '',
186-
}}
187-
>
188-
<Text
189-
style={{
190-
textDecorationLine: item.key === selected ? 'underline' : '',
191-
...styles.text,
192-
}}
193-
>
194-
{item.key}
195-
</Text>
196-
</PlatformPressable>
197-
) : (
198-
<Text style={styles.text}>
199-
{item.key === selected ? 'Selected: ' : ''}
200-
{item.key}
201-
</Text>
202-
)}
203-
</React.Fragment>
204-
))}
205-
<Button
206-
onPress={() => setIsSelectionModeEnabled(!isSelectionModeEnabled)}
207-
>
208-
Toggle selection mode
209-
</Button>
210-
<Text>Selection mode: {isSelectionModeEnabled ? 'ON' : 'OFF'}</Text>
211-
</View>
212-
);
213-
// codeblock-focus-start
214-
215-
// ...
216-
}
217-
// codeblock-focus-end
218-
219-
export default function App() {
220-
return (
221-
<NavigationContainer>
222-
<Stack.Navigator>
223-
<Stack.Screen
224-
name="CustomScreen"
225-
component={ScreenWithCustomBackBehavior}
226-
/>
227-
</Stack.Navigator>
228-
</NavigationContainer>
229-
);
230-
}
231-
232-
const styles = StyleSheet.create({
233-
container: {
234-
flex: 1,
235-
alignItems: 'center',
236-
justifyContent: 'center',
237-
},
238-
text: {
239-
fontSize: 20,
240-
marginBottom: 20,
241-
},
242-
});
243-
```
244-
245-
</TabItem>
246-
</Tabs>
247-
248123
The presented approach will work well for screens that are shown in a `StackNavigator`. Custom back button handling in other situations may not be supported at the moment (eg. A known case when this does not work is when you want to handle back button press in an open drawer. PRs for such use cases are welcome!).
249124

250125
If instead of overriding system back button, you'd like to prevent going back from the screen, see docs for [preventing going back](preventing-going-back.md).

versioned_docs/version-8.x/custom-navigators.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ title: Custom navigators
44
sidebar_label: Custom navigators
55
---
66

7-
import Tabs from '@theme/Tabs';
8-
import TabItem from '@theme/TabItem';
9-
107
In essence, a navigator is a React component that takes a set of screens and options, and renders them based on its [navigation state](navigation-state.md), generally with additional UI such as headers, tab bars, or drawers.
118

129
React Navigation provides a few built-in navigators, but they might not always fit your needs if you want a very custom behavior or UI. In such cases, you can build your own custom navigators using React Navigation's APIs.

versioned_docs/version-8.x/customizing-bottom-tabs.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ title: Customizing bottom tab bar
44
sidebar_label: Customizing tab bar
55
---
66

7-
import Tabs from '@theme/Tabs';
8-
import TabItem from '@theme/TabItem';
9-
107
This guide covers customizing the tab bar in [`createBottomTabNavigator`](bottom-tab-navigator.md). Make sure to install and configure the library according to the [installation instructions](bottom-tab-navigator.md#installation) first.
118

129
## Add icons for each tab

versioned_docs/version-8.x/drawer-actions.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ title: DrawerActions reference
44
sidebar_label: DrawerActions
55
---
66

7-
import Tabs from '@theme/Tabs';
8-
import TabItem from '@theme/TabItem';
9-
107
`DrawerActions` is an object containing methods for generating actions specific to drawer-based navigators. Its methods expand upon the actions available in [CommonActions](navigation-actions.md).
118

129
The following actions are supported:

versioned_docs/version-8.x/elements.md

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ export default function App() {
5050

5151
To use the header in a navigator, you can use the `header` option in the screen options:
5252

53-
<Tabs groupId="config" queryString="config">
54-
<TabItem value="static" label="Static" default>
55-
56-
```js name="Header with Native Stack" snack
53+
```js name="Header with Native Stack" snack static2dynamic
5754
import * as React from 'react';
5855
import { Text, View, Button } from 'react-native';
5956
import { createStaticNavigation } from '@react-navigation/native';
@@ -94,58 +91,6 @@ export default function App() {
9491
}
9592
```
9693

97-
</TabItem>
98-
<TabItem value="dynamic" label="Dynamic">
99-
100-
```js name="Header with Native Stack" snack
101-
import * as React from 'react';
102-
import { Text, View, Button } from 'react-native';
103-
import { NavigationContainer } from '@react-navigation/native';
104-
import { createNativeStackNavigator } from '@react-navigation/native-stack';
105-
// codeblock-focus-start
106-
import { Header, getHeaderTitle } from '@react-navigation/elements';
107-
108-
const Stack = createNativeStackNavigator();
109-
110-
function MyStack() {
111-
return (
112-
<Stack.Navigator
113-
screenOptions={{
114-
header: ({ options, route, back }) => (
115-
<Header
116-
{...options}
117-
back={back}
118-
title={getHeaderTitle(options, route.name)}
119-
/>
120-
),
121-
}}
122-
>
123-
<Stack.Screen name="Home" component={HomeScreen} />
124-
</Stack.Navigator>
125-
);
126-
}
127-
// codeblock-focus-end
128-
129-
function HomeScreen() {
130-
return (
131-
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
132-
<Text>Home Screen</Text>
133-
</View>
134-
);
135-
}
136-
137-
export default function App() {
138-
return (
139-
<NavigationContainer>
140-
<MyStack />
141-
</NavigationContainer>
142-
);
143-
}
144-
```
145-
146-
</TabItem>
147-
</Tabs>
148-
14994
:::note
15095

15196
This doesn't replicate the behavior of the header in stack and native stack navigators as the stack navigator also includes animations, and the native stack navigator header is provided by the native platform.

versioned_docs/version-8.x/function-after-focusing-screen.md

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ With this approach, we will only be able to call an action when the screen focus
2323

2424
Example:
2525

26-
<Tabs groupId="config" queryString="config">
27-
<TabItem value="static" label="Static" default>
28-
29-
```js name="Focus event listener" snack
26+
```js name="Focus event listener" snack static2dynamic
3027
// codeblock-focus-start
3128
import * as React from 'react';
3229
import { View } from 'react-native';
@@ -82,58 +79,6 @@ export default function App() {
8279
}
8380
```
8481

85-
</TabItem>
86-
<TabItem value="dynamic" label="Dynamic">
87-
88-
```js name="Focus event listener" snack
89-
// codeblock-focus-start
90-
import * as React from 'react';
91-
import { View } from 'react-native';
92-
93-
// codeblock-focus-end
94-
import { NavigationContainer, useNavigation } from '@react-navigation/native';
95-
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
96-
97-
// codeblock-focus-start
98-
function ProfileScreen() {
99-
const navigation = useNavigation('Profile');
100-
101-
React.useEffect(() => {
102-
const unsubscribe = navigation.addListener('focus', () => {
103-
alert('Screen is focused');
104-
// The screen is focused
105-
// Call any action
106-
});
107-
108-
// Return the function to unsubscribe from the event so it gets removed on unmount
109-
return unsubscribe;
110-
}, [navigation]);
111-
112-
return <View />;
113-
}
114-
// codeblock-focus-end
115-
116-
function HomeScreen() {
117-
return <View />;
118-
}
119-
120-
const Tab = createBottomTabNavigator();
121-
122-
export default function App() {
123-
return (
124-
<NavigationContainer>
125-
<Tab.Navigator>
126-
<Tab.Screen name="Home" component={HomeScreen} />
127-
<Tab.Screen name="Profile" component={ProfileScreen} />
128-
</Tab.Navigator>
129-
</NavigationContainer>
130-
);
131-
}
132-
```
133-
134-
</TabItem>
135-
</Tabs>
136-
13782
See the [navigation events guide](navigation-events.md) for more details on the event listener API.
13883

13984
In most cases, it's recommended to use the `useFocusEffect` hook instead of adding the listener manually. See below for details.
@@ -144,10 +89,7 @@ React Navigation provides a [hook](use-focus-effect.md) that runs an effect when
14489

14590
This is particularly handy when we are trying to stop something when the page is unfocused, like stopping a video or audio file from playing, or stopping the tracking of a user's location.
14691

147-
<Tabs groupId="config" queryString="config">
148-
<TabItem value="static" label="Static" default>
149-
150-
```js name="useFocusEffect hook" snack
92+
```js name="useFocusEffect hook" snack static2dynamic
15193
import * as React from 'react';
15294
import { View } from 'react-native';
15395
import {
@@ -198,54 +140,6 @@ export default function App() {
198140
}
199141
```
200142

201-
</TabItem>
202-
<TabItem value="dynamic" label="Dynamic">
203-
204-
```js name="useFocusEffect hook" snack
205-
import * as React from 'react';
206-
import { View } from 'react-native';
207-
import { NavigationContainer, useFocusEffect } from '@react-navigation/native';
208-
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
209-
210-
// codeblock-focus-start
211-
function ProfileScreen() {
212-
useFocusEffect(
213-
React.useCallback(() => {
214-
alert('Screen was focused');
215-
// Do something when the screen is focused
216-
return () => {
217-
alert('Screen was unfocused');
218-
// Do something when the screen is unfocused
219-
// Useful for cleanup functions
220-
};
221-
}, [])
222-
);
223-
224-
return <View />;
225-
}
226-
// codeblock-focus-end
227-
228-
function HomeScreen() {
229-
return <View />;
230-
}
231-
232-
const Tab = createBottomTabNavigator();
233-
234-
export default function App() {
235-
return (
236-
<NavigationContainer>
237-
<Tab.Navigator>
238-
<Tab.Screen name="Home" component={HomeScreen} />
239-
<Tab.Screen name="Profile" component={ProfileScreen} />
240-
</Tab.Navigator>
241-
</NavigationContainer>
242-
);
243-
}
244-
```
245-
246-
</TabItem>
247-
</Tabs>
248-
249143
See the [`useFocusEffect`](https://reactnavigation.org/docs/use-focus-effect/) documentation for more details.
250144

251145
## Re-rendering screen with the `useIsFocused` hook

versioned_docs/version-8.x/header-buttons.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ title: Header buttons
44
sidebar_label: Header buttons
55
---
66

7-
import Tabs from '@theme/Tabs';
8-
import TabItem from '@theme/TabItem';
9-
107
Now that we know how to customize the look of our headers, let's make them interactive!
118

129
## Adding a button to the header

0 commit comments

Comments
 (0)