Skip to content

Commit c104934

Browse files
authored
Update snacks in guides (#598)
* Update snacks in guides * Add minor fixes
1 parent a5179d7 commit c104934

34 files changed

+1462
-92
lines changed

docs/drawer-based-navigation.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Before continuing, first install [`@react-navigation/drawer`](https://github.com
1414

1515
To use this drawer navigator, import it from `@react-navigation/drawer`:
1616
(swipe right to open)
17+
1718
<samp id="drawer-based-navigation" />
1819

1920
```js
@@ -24,7 +25,7 @@ import { NavigationNativeContainer } from '@react-navigation/native';
2425

2526
function HomeScreen({ navigation }) {
2627
return (
27-
<View style={{ flex: 1, flexDirection: 'column-reverse' }}>
28+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
2829
<Button
2930
onPress={() => navigation.navigate('Notifications')}
3031
title="Go to notifications"
@@ -35,7 +36,7 @@ function HomeScreen({ navigation }) {
3536

3637
function NotificationsScreen({ navigation }) {
3738
return (
38-
<View style={{ flex: 1, flexDirection: 'column-reverse' }}>
39+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
3940
<Button onPress={() => navigation.goBack()} title="Go back home" />
4041
</View>
4142
);
@@ -57,7 +58,7 @@ export default function App() {
5758

5859
## Opening and closing drawer
5960

60-
To open and close drawer, use the following helpers to open and close the drawer:
61+
To open and close drawer, use the following helpers:
6162

6263
```js
6364
navigation.openDrawer();

docs/function-after-focusing-screen.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ With this approach, we will only be able to call an action when the screen focus
2020

2121
Example:
2222

23+
<samp id="focus-event-listener" />
24+
2325
```js
2426
import * as React from 'react';
2527
import { View } from 'react-native';
2628

27-
export default function TabScreen({ navigation }) {
29+
function ProfileScreen({ navigation }) {
2830
React.useEffect(() => {
2931
const unsubscribe = navigation.addListener('focus', () => {
3032
// The screen is focused
@@ -47,6 +49,8 @@ React Navigation provides a [hook](https://reactjs.org/docs/hooks-intro.html) th
4749

4850
This is particularly handy when we are trying to stop something when the page is unfocused, like stopping a video or audio file from playing, or stopping the tracking of a user's location.
4951

52+
<samp id="simple-focus-effect" />
53+
5054
```js
5155
import { useFocusEffect } from '@react-navigation/native';
5256

@@ -73,6 +77,8 @@ The hook will return `true` when the screen is focused and `false` when our comp
7377

7478
The `useIsFocused` hook will cause our component to re-render when we focus and unfocus a screen. Using this hook component may introduce unnecessary component re-renders as a screen comes in and out of focus. This could cause issues depending on the type of action we're calling on focusing. Hence we recommend to use this hook only if you need to trigger a re-render. For side-effects such as subscribing to events or fetching data, use the methods described above.
7579

80+
<samp id="use-is-focused" />
81+
7682
```js
7783
import * as React from 'react';
7884
import { Text } from 'react-native';
@@ -86,4 +92,4 @@ function Profile() {
8692
}
8793
```
8894

89-
This example is also documented in the <a href="/docs/use-is-focused.html">`useIsFocused` API documentation</a>.
95+
This example is also documented in the [`useIsFocused` API documentation](use-is-focused.md).

docs/handling-safe-area.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ To fix this you can, once again, apply safe area insets your content. This will
128128

129129
In some cases you might need more control over which paddings are applied. For example, you can only apply the top and the bottom padding by changing the `style` object:
130130

131+
<samp id="use-safe-area" />
132+
131133
```jsx
132134
import { useSafeArea } from 'react-native-safe-area-context';
133135

docs/localization.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,18 @@ console.log(Localization.locale);
3636
Next let's store our `locale` in the state of our root app component and then thread it through `LocalizationContext` to make it available throughout our app.
3737

3838
```js
39-
export const LocalizationContext = React,createContext();
39+
export const LocalizationContext = React.createContext();
4040

4141
export default function App() {
4242
const [locale, setLocale] = React.useState(Localization.locale);
43-
const localizationContext = React.useMemo(() => ({
44-
t: (scope, options) => i18n.t(scope, { locale, ...options }),
45-
locale,
46-
setlocale,
47-
}), [locale]);
43+
const localizationContext = React.useMemo(
44+
() => ({
45+
t: (scope, options) => i18n.t(scope, { locale, ...options }),
46+
locale,
47+
setLocale,
48+
}),
49+
[locale]
50+
);
4851

4952
return (
5053
<LocalizationContext.Provider value={localizationContext}>
@@ -58,7 +61,9 @@ export default function App() {
5861

5962
Now in our screens we can use these `LocalizationContext` as follows:
6063

61-
```jsx
64+
<samp id="localization" />
65+
66+
```js
6267
function MyScreen() {
6368
const { t, locale, setLocale } = React.useContext(LocalizationContext);
6469

@@ -83,6 +88,8 @@ function MyScreen() {
8388

8489
We can also use it for screen options:
8590

91+
<samp id="localization-with-title" />
92+
8693
```js
8794
function MyStack() {
8895
const { t } = React.useContext(LocalizationContext);
@@ -99,4 +106,4 @@ function MyStack() {
99106
}
100107
```
101108

102-
Refer to [themes](themes.html) and the [React documentation on context](https://reactjs.org/docs/context.html) for help.
109+
Refer to [themes](themes.md) and the [React documentation on context](https://reactjs.org/docs/context.html) for help.

docs/navigating-without-navigation-prop.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Calling functions such as `navigate` or `popToTop` on the `navigation` prop is n
88

99
You can get access to the root navigation object through a `ref` and pass it to the `NavigationService` which we will later use to navigate.
1010

11-
```javascript
11+
```js
1212
// App.js
1313

1414
import { NavigationNativeContainer } from '@react-navigation/native';
@@ -25,7 +25,7 @@ export default function App() {
2525

2626
In the next step, we define `NavigationService` which is a simple module with functions that dispatch user-defined navigation actions.
2727

28-
```javascript
28+
```js
2929
// NavigationService.js
3030

3131
import * as React from 'react';
@@ -41,7 +41,9 @@ export function navigate(name, params) {
4141

4242
Then, in any of your javascript modules, just import the `NavigationService` and call functions which you exported from it. You may use this approach outside of your React components and, in fact, it works just as well when used from within them.
4343

44-
```javascript
44+
<samp id="no-nav-prop" />
45+
46+
```js
4547
// any js module
4648
import * as NavigationService from './path/to/NavigationService.js';
4749

docs/redux-integration.md

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Redux integration
44
sidebar_label: Redux integration
55
---
66

7-
It is extremely easy to use Redux in an app with React Navigation. It's basically no different than without React Navigation. The following example shows how to do it end to end: https://snack.expo.io/@react-navigation/redux-example. The most important piece from it is the following:
7+
It is extremely easy to use Redux in an app with React Navigation. It's basically no different than without React Navigation.
88

99
```js
1010
import { Provider } from 'react-redux';
@@ -28,12 +28,14 @@ Notice that we wrap our components in a `Provider` like we'd normally do with `r
2828

2929
Create a component, `connect` it to the store, then use that component in the `title`.
3030

31+
<samp id="redux-integration" />
32+
3133
```js
32-
function Count({ value }) {
34+
function Counter({ value }) {
3335
return <Text>Count: {value}</Text>;
3436
}
3537

36-
const CountContainer = connect(state => ({ value: state.count }))(Count);
38+
const CounterContainer = connect(state => ({ value: state.count }))(Counter);
3739
```
3840

3941
```js
@@ -50,10 +52,10 @@ If the value isn't expected to change, you can just pass it from a `connect`ed c
5052

5153
```js
5254
<Button
53-
title="Go to static count screen"
55+
title="Go to static counter screen"
5456
onPress={() =>
5557
props.navigation.navigate('StaticCounter', {
56-
count: props.count,
58+
count,
5759
})
5860
}
5961
/>
@@ -69,34 +71,16 @@ function StaticCounter({ route }) {
6971
}
7072
```
7173

74+
<samp id="redux-integration-nav-param" />
75+
7276
```js
73-
<Screen
74-
name="Counter"
77+
<RootStack.Screen
78+
name="StaticCounter"
7579
component={StaticCounter}
76-
options={({ route }) => ({ title: () => route.params.count })}
80+
options={({ route }) => ({ title: route.params.count })}
7781
/>
7882
```
7983

80-
### setParams from your screen
81-
82-
Let's modify the `StaticCounter` from the previous example as follows:
83-
84-
```js
85-
function StaticCounter({ count, route, navigation }) {
86-
React.useEffect(() => {
87-
navigation.setParams({ count });
88-
}, [navigation, count]);
89-
90-
return (
91-
<View style={styles.container}>
92-
<Text style={styles.paragraph}>{route.params.count}</Text>
93-
</View>
94-
);
95-
}
96-
```
97-
98-
Now whenever the store updates we update the `count` param and the title updates accordingly.
99-
10084
## Can I store the navigation state in Redux too?
10185

10286
This is not possible. We don't support it because it's too easy to shoot yourself in the foot and slow down / break your app.

docs/screen-options-resolution.md

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,58 +12,70 @@ In this document we'll explain how this works when there are multiple navigators
1212

1313
Let's take for example a tab navigator that contains a stack in each tab. What happens if we set the `options` on a screen inside of the stack?
1414

15+
<samp id="stack-in-tab-nav-options" />
16+
1517
```js
1618
const Tab = createTabNavigator();
1719
const HomeStack = createStackNavigator();
1820
const SettingsStack = createStackNavigator();
1921

2022
function HomeStackScreen() {
2123
return (
22-
<Tab.Navigator>
23-
<Tab.Screen name="A" component={A} options={{ tabBarLabel: 'Home!' }} />
24-
</Tab.Navigator>
24+
<HomeStack.Navigator>
25+
<HomeStack.Screen
26+
name="A"
27+
component={A}
28+
options={{ tabBarLabel: 'Home!' }}
29+
/>
30+
</HomeStack.Navigator>
2531
);
2632
}
2733

2834
function SettingsStackScreen() {
2935
return (
30-
<Tab.Navigator>
31-
<Tab.Screen
36+
<SettingsStack.Navigator>
37+
<SettingsStack.Screen
3238
name="B"
3339
component={B}
3440
options={{ tabBarLabel: 'Settings!' }}
3541
/>
36-
</Tab.Navigator>
42+
</SettingsStack.Navigator>
3743
);
3844
}
3945

4046
export default function App() {
4147
return (
42-
<Tab.Navigator>
43-
<Tab.Screen name="Home" component={HomeStackScreen} />
44-
<Tab.Screen name="Settings" component={SettingsStack} />
45-
</Tab.Navigator>
48+
<NavigationNativeContainer>
49+
<Tab.Navigator>
50+
<Tab.Screen name="Home" component={HomeStackScreen} />
51+
<Tab.Screen name="Settings" component={SettingsStackScreen} />
52+
</Tab.Navigator>
53+
</NavigationNativeContainer>
4654
);
4755
}
4856
```
4957

5058
As we mentioned earlier, you can only modify navigation options for a navigator from one of its screen components. `A` and `B` above are screen components in `HomeStack` and `SettingsStack` respectively, not in the tab navigator. So the result will be that the `tabBarLabel` property is not applied to the tab navigator. We can fix this though!
5159

60+
<samp id="stack-in-tab-nav-options-fixed" />
61+
5262
```js
5363
export default function App() {
5464
return (
55-
<Tab.Navigator>
56-
<Tab.Screen
57-
name="Home"
58-
component={HomeStackScreen}
59-
options={{ tabBarLabel: 'Home!' }}
60-
/>
61-
<Tab.Screen
62-
name="Settings"
63-
component={SettingsStack}
64-
options={{ tabBarLabel: 'Settings!' }}
65-
/>
66-
</Tab.Navigator>
65+
<NavigationNativeContainer>
66+
<Tab.Navigator>
67+
<Tab.Screen
68+
name="Home"
69+
component={HomeStackScreen}
70+
options={{ tabBarLabel: 'Home!' }}
71+
/>
72+
<Tab.Screen
73+
name="Settings"
74+
component={SettingsStackScreen}
75+
options={{ tabBarLabel: 'Settings!' }}
76+
/>
77+
</Tab.Navigator>
78+
</NavigationNativeContainer>
6779
);
6880
}
6981
```
@@ -74,6 +86,8 @@ When we set the `options` directly on `Screen` components containing the `HomeSt
7486

7587
Imagine the following configuration:
7688

89+
<samp id="parent-options-from-child-start" />
90+
7791
```js
7892
const Tab = createBottomTabNavigator();
7993

@@ -89,12 +103,14 @@ function HomeTabs() {
89103

90104
const Stack = createStackNavigator();
91105

92-
function App() {
106+
export default function App() {
93107
return (
94-
<Stack.Navigator>
95-
<Stack.Screen name="Home" component={HomeTabs} />
96-
<Stack.Screen name="Settings" component={SettingsScreen} />
97-
</Stack.Navigator>
108+
<NavigationNativeContainer>
109+
<Stack.Navigator>
110+
<Stack.Screen name="Home" component={HomeTabs} />
111+
<Stack.Screen name="Settings" component={SettingsScreen} />
112+
</Stack.Navigator>
113+
</NavigationNativeContainer>
98114
);
99115
}
100116
```
@@ -127,6 +143,7 @@ function getHeaderTitle(route) {
127143
Then we can use this function in 2 ways:
128144

129145
1. Using `options` prop on `Screen` (recommended):
146+
<samp id="parent-options-from-child-opt1" />
130147

131148
```js
132149
<Stack.Screen
@@ -139,6 +156,7 @@ Then we can use this function in 2 ways:
139156
```
140157

141158
2. Using `navigation.setOptions`:
159+
<samp id="parent-options-from-child-opt2" />
142160

143161
```js
144162
function HomeTabs({ navigation, route }) {
@@ -168,6 +186,8 @@ In many cases, similar behavior can be achieved by reorganizing our navigators.
168186

169187
For example, for the above use case, instead of adding a tab navigator inside a stack navigator, we can add a stack navigator inside each of the tabs.
170188

189+
<samp id="reorganized-navigators" />
190+
171191
```js
172192
const FeedStack = createStackNavigator();
173193

docs/screen-tracking.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ To get notified of state changes, we can use the `onStateChange` prop on `Naviga
1515

1616
This example shows how to do screen tracking and send to Google Analytics. The approach can be adapted to any other analytics SDK.
1717

18+
<samp id="screen-tracking-for-analytics" />
19+
1820
```js
1921
import * as React from 'react';
2022
import analytics from '@react-native-firebase/analytics';

0 commit comments

Comments
 (0)