Skip to content

Commit 34b2a64

Browse files
Add static examples
1 parent da8957a commit 34b2a64

File tree

1 file changed

+122
-3
lines changed

1 file changed

+122
-3
lines changed

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

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,138 @@ 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>
18+
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
24+
import {
25+
useNavigation,
26+
createStaticNavigation,
27+
NavigationContext,
28+
} from '@react-navigation/native';
29+
// codeblock-focus-end
30+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
31+
32+
function HomeScreen() {
33+
return <SomeComponent />;
34+
}
35+
36+
// codeblock-focus-start
37+
38+
function SomeComponent() {
39+
// We can access navigation object via context
40+
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+
);
51+
}
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);
1472

15-
```js
16-
import { NavigationContext } from '@react-navigation/native';
73+
function App() {
74+
return <Navigation />;
75+
}
76+
77+
export default App;
78+
```
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 {
89+
NavigationContainer,
90+
NavigationContext,
91+
} from '@react-navigation/native';
92+
// codeblock-focus-end
93+
import { createNativeStackNavigator } from '@react-navigation/native-stack';
94+
95+
function HomeScreen() {
96+
return <SomeComponent />;
97+
}
98+
99+
// codeblock-focus-start
17100

18101
function SomeComponent() {
19102
// We can access navigation object via context
20103
const navigation = React.useContext(NavigationContext);
104+
// codeblock-focus-end
105+
106+
return (
107+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
108+
<Text>Some component inside HomeScreen</Text>
109+
<Button onPress={() => navigation.navigate('Profile')}>
110+
Go to Profile
111+
</Button>
112+
</View>
113+
);
114+
}
115+
116+
function ProfileScreen({ navigation }) {
117+
return (
118+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
119+
<Button onPress={() => navigation.goBack()}>Go back</Button>
120+
</View>
121+
);
122+
}
123+
124+
const Stack = createNativeStackNavigator();
125+
126+
function App() {
127+
return (
128+
<NavigationContainer>
129+
<Stack.Navigator initialRouteName="Home">
130+
<Stack.Screen name="Home" component={HomeScreen} />
131+
<Stack.Screen name="Profile" component={ProfileScreen} />
132+
</Stack.Navigator>
133+
</NavigationContainer>
134+
);
21135
}
136+
137+
export default App;
22138
```
139+
140+
</TabItem>
141+
</Tabs>

0 commit comments

Comments
 (0)