Skip to content

Commit 1fb2545

Browse files
Migrate 'Navigation context' examples to static API in v7 (#1325)
1 parent f6a335d commit 1fb2545

File tree

1 file changed

+119
-2
lines changed

1 file changed

+119
-2
lines changed

versioned_docs/version-7.x/navigation-context.md

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,136 @@ title: NavigationContext
44
sidebar_label: NavigationContext
55
---
66

7+
import Tabs from '@theme/Tabs';
8+
import TabItem from '@theme/TabItem';
9+
710
`NavigationContext` provides the `navigation` object (same object as the [navigation](navigation-object.md) prop). In fact, [useNavigation](use-navigation.md) uses this context to get the `navigation` prop.
811

912
Most of the time, you won't use `NavigationContext` directly, as the provided `useNavigation` covers most use cases. But just in case you have something else in mind, `NavigationContext` is available for you to use.
1013

1114
Example:
1215

13-
<samp id="navigation-context" />
16+
<Tabs groupId="config" queryString="config">
17+
<TabItem value="static" label="Static" default>
1418

15-
```js
19+
```js name="Navigation context" snack version=7 dependencies=@react-navigation/elements
20+
import * as React from 'react';
21+
import { View, Text } from 'react-native';
22+
import { Button } from '@react-navigation/elements';
23+
// codeblock-focus-start
1624
import { NavigationContext } from '@react-navigation/native';
25+
// codeblock-focus-end
26+
import {
27+
useNavigation,
28+
createStaticNavigation,
29+
} from '@react-navigation/native';
30+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
31+
32+
function HomeScreen() {
33+
return <SomeComponent />;
34+
}
35+
36+
// codeblock-focus-start
1737

1838
function SomeComponent() {
1939
// We can access navigation object via context
2040
const navigation = React.useContext(NavigationContext);
41+
// codeblock-focus-end
42+
43+
return (
44+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
45+
<Text>Some component inside HomeScreen</Text>
46+
<Button onPress={() => navigation.navigate('Profile')}>
47+
Go to Profile
48+
</Button>
49+
</View>
50+
);
2151
}
52+
53+
function ProfileScreen() {
54+
const navigation = useNavigation();
55+
56+
return (
57+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
58+
<Button onPress={() => navigation.goBack()}>Go back</Button>
59+
</View>
60+
);
61+
}
62+
63+
const Stack = createNativeStackNavigator({
64+
initialRouteName: 'Home',
65+
screens: {
66+
Home: HomeScreen,
67+
Profile: ProfileScreen,
68+
},
69+
});
70+
71+
const Navigation = createStaticNavigation(Stack);
72+
73+
function App() {
74+
return <Navigation />;
75+
}
76+
77+
export default App;
2278
```
79+
80+
</TabItem>
81+
<TabItem value="dynamic" label="Dynamic" default>
82+
83+
```js name="Navigation context" snack version=7 dependencies=@react-navigation/elements
84+
import * as React from 'react';
85+
import { View, Text } from 'react-native';
86+
import { Button } from '@react-navigation/elements';
87+
// codeblock-focus-start
88+
import { NavigationContext } from '@react-navigation/native';
89+
// codeblock-focus-end
90+
import { NavigationContainer } from '@react-navigation/native';
91+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
92+
93+
function HomeScreen() {
94+
return <SomeComponent />;
95+
}
96+
97+
// codeblock-focus-start
98+
99+
function SomeComponent() {
100+
// We can access navigation object via context
101+
const navigation = React.useContext(NavigationContext);
102+
// codeblock-focus-end
103+
104+
return (
105+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
106+
<Text>Some component inside HomeScreen</Text>
107+
<Button onPress={() => navigation.navigate('Profile')}>
108+
Go to Profile
109+
</Button>
110+
</View>
111+
);
112+
}
113+
114+
function ProfileScreen({ navigation }) {
115+
return (
116+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
117+
<Button onPress={() => navigation.goBack()}>Go back</Button>
118+
</View>
119+
);
120+
}
121+
122+
const Stack = createNativeStackNavigator();
123+
124+
function App() {
125+
return (
126+
<NavigationContainer>
127+
<Stack.Navigator initialRouteName="Home">
128+
<Stack.Screen name="Home" component={HomeScreen} />
129+
<Stack.Screen name="Profile" component={ProfileScreen} />
130+
</Stack.Navigator>
131+
</NavigationContainer>
132+
);
133+
}
134+
135+
export default App;
136+
```
137+
138+
</TabItem>
139+
</Tabs>

0 commit comments

Comments
 (0)