@@ -16,52 +16,90 @@ The most common way to interact with a header is by tapping on a button either t
16
16
<Tabs groupId =" config " queryString =" config " >
17
17
<TabItem value =" static " label =" Static " default >
18
18
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 ({
21
35
screens: {
22
36
Home: {
23
37
screen : HomeScreen,
24
38
options: {
25
- headerTitle : ( props ) => < LogoTitle { ... props} / > ,
39
+ // highlight-start
26
40
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" / >
32
42
),
43
+ // highlight-end
33
44
},
34
45
},
35
46
},
36
47
});
48
+ // codeblock-focus-end
49
+
50
+ const Navigation = createStaticNavigation (MyStack);
51
+
52
+ export default function App () {
53
+ return < Navigation / > ;
54
+ }
37
55
```
38
56
39
57
</TabItem >
40
58
<TabItem value =" dynamic " label =" Dynamic " >
41
59
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' ;
43
65
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
45
77
function MyStack () {
46
78
return (
47
79
< Stack .Navigator >
48
80
< Stack .Screen
49
81
name= " Home"
50
82
component= {HomeScreen}
51
83
options= {{
52
- headerTitle : ( props ) => < LogoTitle { ... props} / > ,
84
+ // highlight-start
53
85
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" / >
59
87
),
88
+ // highlight-end
60
89
}}
61
90
/ >
62
91
< / Stack .Navigator >
63
92
);
64
93
}
94
+ // codeblock-focus-end
95
+
96
+ export default function App () {
97
+ return (
98
+ < NavigationContainer>
99
+ < MyStack / >
100
+ < / NavigationContainer>
101
+ );
102
+ }
65
103
```
66
104
67
105
</TabItem >
@@ -82,73 +120,110 @@ In some cases, components in the header need to interact with the screen compone
82
120
<Tabs groupId =" config " queryString =" config " >
83
121
<TabItem value =" static " label =" Static " default >
84
122
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 ({
87
153
screens: {
88
154
Home: {
89
155
screen : HomeScreen,
90
156
options: {
91
- headerTitle : (props ) => < LogoTitle {... props} / > ,
92
157
// Add a placeholder button without the `onPress` to avoid flicker
158
+ // highlight-next-line
93
159
headerRight : () => < Button title= " Update count" / > ,
94
160
},
95
161
},
96
162
},
97
163
});
164
+ // codeblock-focus-end
98
165
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 ();
100
187
const [count , setCount ] = React .useState (0 );
101
188
102
189
React .useEffect (() => {
103
190
// Use `setOptions` to update the button that we previously specified
104
191
// Now the button includes an `onPress` handler to update the count
192
+ // highlight-start
105
193
navigation .setOptions ({
106
194
headerRight : () => (
107
195
< Button onPress= {() => setCount ((c ) => c + 1 )} title= " Update count" / >
108
196
),
109
197
});
198
+ // highlight-end
110
199
}, [navigation]);
111
200
112
201
return < Text > Count: {count}< / Text > ;
113
202
}
114
- ```
115
-
116
- </TabItem >
117
- <TabItem value =" dynamic " label =" Dynamic " >
118
-
119
- <samp id =" header-interaction " >header interaction</samp >
120
203
121
- ``` js
122
204
function MyStack () {
123
205
return (
124
206
< Stack .Navigator >
125
207
< Stack .Screen
126
208
name= " Home"
127
209
component= {HomeScreen}
128
- options= {({ navigation, route }) => ({
129
- headerTitle : (props ) => < LogoTitle {... props} / > ,
210
+ options= {{
130
211
// Add a placeholder button without the `onPress` to avoid flicker
212
+ // highlight-next-line
131
213
headerRight : () => < Button title= " Update count" / > ,
132
- }) }
214
+ }}
133
215
/ >
134
216
< / Stack .Navigator >
135
217
);
136
218
}
219
+ // codeblock-focus-end
137
220
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
+ );
152
227
}
153
228
```
154
229
0 commit comments