Skip to content

Commit 2746c52

Browse files
committed
Add snack links to header buttons
1 parent f82b153 commit 2746c52

File tree

1 file changed

+119
-44
lines changed

1 file changed

+119
-44
lines changed

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

Lines changed: 119 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,90 @@ The most common way to interact with a header is by tapping on a button either t
1616
<Tabs groupId="config" queryString="config">
1717
<TabItem value="static" label="Static" default>
1818

19-
```js
20-
const MyStack = createStackNavigator({
19+
```js name="Header button" snack version=7
20+
import * as React from 'react';
21+
import { Text, View, Button } from 'react-native';
22+
import { createStaticNavigation } from '@react-navigation/native';
23+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
24+
25+
function HomeScreen() {
26+
return (
27+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
28+
<Text>Home Screen</Text>
29+
</View>
30+
);
31+
}
32+
33+
// codeblock-focus-start
34+
const MyStack = createNativeStackNavigator({
2135
screens: {
2236
Home: {
2337
screen: HomeScreen,
2438
options: {
25-
headerTitle: (props) => <LogoTitle {...props} />,
39+
// highlight-start
2640
headerRight: () => (
27-
<Button
28-
onPress={() => alert('This is a button!')}
29-
title="Info"
30-
color="#fff"
31-
/>
41+
<Button onPress={() => alert('This is a button!')} title="Info" />
3242
),
43+
// highlight-end
3344
},
3445
},
3546
},
3647
});
48+
// codeblock-focus-end
49+
50+
const Navigation = createStaticNavigation(MyStack);
51+
52+
export default function App() {
53+
return <Navigation />;
54+
}
3755
```
3856

3957
</TabItem>
4058
<TabItem value="dynamic" label="Dynamic">
4159

42-
<samp id="simple-header-button">header button</samp>
60+
```js name="Header button" snack version=7
61+
import * as React from 'react';
62+
import { Text, View, Button } from 'react-native';
63+
import { NavigationContainer } from '@react-navigation/native';
64+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
4365

44-
```js
66+
function HomeScreen() {
67+
return (
68+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
69+
<Text>Home Screen</Text>
70+
</View>
71+
);
72+
}
73+
74+
const Stack = createNativeStackNavigator();
75+
76+
// codeblock-focus-start
4577
function MyStack() {
4678
return (
4779
<Stack.Navigator>
4880
<Stack.Screen
4981
name="Home"
5082
component={HomeScreen}
5183
options={{
52-
headerTitle: (props) => <LogoTitle {...props} />,
84+
// highlight-start
5385
headerRight: () => (
54-
<Button
55-
onPress={() => alert('This is a button!')}
56-
title="Info"
57-
color="#fff"
58-
/>
86+
<Button onPress={() => alert('This is a button!')} title="Info" />
5987
),
88+
// highlight-end
6089
}}
6190
/>
6291
</Stack.Navigator>
6392
);
6493
}
94+
// codeblock-focus-end
95+
96+
export default function App() {
97+
return (
98+
<NavigationContainer>
99+
<MyStack />
100+
</NavigationContainer>
101+
);
102+
}
65103
```
66104

67105
</TabItem>
@@ -82,73 +120,110 @@ In some cases, components in the header need to interact with the screen compone
82120
<Tabs groupId="config" queryString="config">
83121
<TabItem value="static" label="Static" default>
84122

85-
```js
86-
const MyStack = createStackNavigator({
123+
```js name="Header button" snack version=7
124+
import * as React from 'react';
125+
import { Text, View, Button } from 'react-native';
126+
import {
127+
createStaticNavigation,
128+
useNavigation,
129+
} from '@react-navigation/native';
130+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
131+
132+
// codeblock-focus-start
133+
function HomeScreen() {
134+
const navigation = useNavigation();
135+
const [count, setCount] = React.useState(0);
136+
137+
React.useEffect(() => {
138+
// Use `setOptions` to update the button that we previously specified
139+
// Now the button includes an `onPress` handler to update the count
140+
// highlight-start
141+
navigation.setOptions({
142+
headerRight: () => (
143+
<Button onPress={() => setCount((c) => c + 1)} title="Update count" />
144+
),
145+
});
146+
// highlight-end
147+
}, [navigation]);
148+
149+
return <Text>Count: {count}</Text>;
150+
}
151+
152+
const MyStack = createNativeStackNavigator({
87153
screens: {
88154
Home: {
89155
screen: HomeScreen,
90156
options: {
91-
headerTitle: (props) => <LogoTitle {...props} />,
92157
// Add a placeholder button without the `onPress` to avoid flicker
158+
// highlight-next-line
93159
headerRight: () => <Button title="Update count" />,
94160
},
95161
},
96162
},
97163
});
164+
// codeblock-focus-end
98165

99-
function HomeScreen({ navigation }) {
166+
const Navigation = createStaticNavigation(MyStack);
167+
168+
export default function App() {
169+
return <Navigation />;
170+
}
171+
```
172+
173+
</TabItem>
174+
<TabItem value="dynamic" label="Dynamic">
175+
176+
```js name="Header button" snack version=7
177+
import * as React from 'react';
178+
import { Text, View, Button } from 'react-native';
179+
import { NavigationContainer, useNavigation } from '@react-navigation/native';
180+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
181+
182+
const Stack = createNativeStackNavigator();
183+
184+
// codeblock-focus-start
185+
function HomeScreen() {
186+
const navigation = useNavigation();
100187
const [count, setCount] = React.useState(0);
101188

102189
React.useEffect(() => {
103190
// Use `setOptions` to update the button that we previously specified
104191
// Now the button includes an `onPress` handler to update the count
192+
// highlight-start
105193
navigation.setOptions({
106194
headerRight: () => (
107195
<Button onPress={() => setCount((c) => c + 1)} title="Update count" />
108196
),
109197
});
198+
// highlight-end
110199
}, [navigation]);
111200

112201
return <Text>Count: {count}</Text>;
113202
}
114-
```
115-
116-
</TabItem>
117-
<TabItem value="dynamic" label="Dynamic">
118-
119-
<samp id="header-interaction">header interaction</samp>
120203

121-
```js
122204
function MyStack() {
123205
return (
124206
<Stack.Navigator>
125207
<Stack.Screen
126208
name="Home"
127209
component={HomeScreen}
128-
options={({ navigation, route }) => ({
129-
headerTitle: (props) => <LogoTitle {...props} />,
210+
options={{
130211
// Add a placeholder button without the `onPress` to avoid flicker
212+
// highlight-next-line
131213
headerRight: () => <Button title="Update count" />,
132-
})}
214+
}}
133215
/>
134216
</Stack.Navigator>
135217
);
136218
}
219+
// codeblock-focus-end
137220

138-
function HomeScreen({ navigation }) {
139-
const [count, setCount] = React.useState(0);
140-
141-
React.useEffect(() => {
142-
// Use `setOptions` to update the button that we previously specified
143-
// Now the button includes an `onPress` handler to update the count
144-
navigation.setOptions({
145-
headerRight: () => (
146-
<Button onPress={() => setCount((c) => c + 1)} title="Update count" />
147-
),
148-
});
149-
}, [navigation]);
150-
151-
return <Text>Count: {count}</Text>;
221+
export default function App() {
222+
return (
223+
<NavigationContainer>
224+
<MyStack />
225+
</NavigationContainer>
226+
);
152227
}
153228
```
154229

0 commit comments

Comments
 (0)