diff --git a/versioned_docs/version-7.x/navigation-context.md b/versioned_docs/version-7.x/navigation-context.md index 7b31dd0b654..928f2b8ba14 100755 --- a/versioned_docs/version-7.x/navigation-context.md +++ b/versioned_docs/version-7.x/navigation-context.md @@ -4,19 +4,136 @@ title: NavigationContext sidebar_label: NavigationContext --- +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + `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. 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. Example: - + + -```js +```js name="Navigation context" snack version=7 dependencies=@react-navigation/elements +import * as React from 'react'; +import { View, Text } from 'react-native'; +import { Button } from '@react-navigation/elements'; +// codeblock-focus-start import { NavigationContext } from '@react-navigation/native'; +// codeblock-focus-end +import { + useNavigation, + createStaticNavigation, +} from '@react-navigation/native'; +import { createNativeStackNavigator } from '@react-navigation/native-stack'; + +function HomeScreen() { + return ; +} + +// codeblock-focus-start function SomeComponent() { // We can access navigation object via context const navigation = React.useContext(NavigationContext); + // codeblock-focus-end + + return ( + + Some component inside HomeScreen + + + ); } + +function ProfileScreen() { + const navigation = useNavigation(); + + return ( + + + + ); +} + +const Stack = createNativeStackNavigator({ + initialRouteName: 'Home', + screens: { + Home: HomeScreen, + Profile: ProfileScreen, + }, +}); + +const Navigation = createStaticNavigation(Stack); + +function App() { + return ; +} + +export default App; ``` + + + + +```js name="Navigation context" snack version=7 dependencies=@react-navigation/elements +import * as React from 'react'; +import { View, Text } from 'react-native'; +import { Button } from '@react-navigation/elements'; +// codeblock-focus-start +import { NavigationContext } from '@react-navigation/native'; +// codeblock-focus-end +import { NavigationContainer } from '@react-navigation/native'; +import { createNativeStackNavigator } from '@react-navigation/native-stack'; + +function HomeScreen() { + return ; +} + +// codeblock-focus-start + +function SomeComponent() { + // We can access navigation object via context + const navigation = React.useContext(NavigationContext); + // codeblock-focus-end + + return ( + + Some component inside HomeScreen + + + ); +} + +function ProfileScreen({ navigation }) { + return ( + + + + ); +} + +const Stack = createNativeStackNavigator(); + +function App() { + return ( + + + + + + + ); +} + +export default App; +``` + + +