Skip to content

Commit e4d4360

Browse files
Migrate 'Troubleshooting' to static API (#1313)
1 parent 89e9479 commit e4d4360

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed

versioned_docs/version-7.x/troubleshooting.md

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ title: Troubleshooting
44
sidebar_label: Troubleshooting
55
---
66

7+
import Tabs from '@theme/Tabs';
8+
import TabItem from '@theme/TabItem';
9+
710
This section attempts to outline issues that users frequently encounter when first getting accustomed to using React Navigation. These issues may or may not be related to React Navigation itself.
811

912
Before troubleshooting an issue, make sure that you have upgraded to **the latest available versions** of the packages. You can install the latest versions by installing the packages again (e.g. `npm install package-name`).
@@ -198,6 +201,30 @@ Now rebuild the app and test on your device or simulator.
198201
199202
If you wrap the container in a `View`, make sure the `View` stretches to fill the container using `flex: 1`:
200203
204+
<Tabs groupId="config" queryString="config">
205+
<TabItem value="static" label="Static" default>
206+
207+
```js
208+
import * as React from 'react';
209+
import { View } from 'react-native';
210+
import { createStaticNavigation } from '@react-navigation/native';
211+
212+
/* ... */
213+
214+
const Navigation = createStaticNavigation(RootStack);
215+
216+
export default function App() {
217+
return (
218+
<View style={{ flex: 1 }}>
219+
<Navigation />
220+
</View>
221+
);
222+
}
223+
```
224+
225+
</TabItem>
226+
<TabItem value="dynamic" label="Dynamic" default>
227+
201228
```js
202229
import * as React from 'react';
203230
import { View } from 'react-native';
@@ -212,6 +239,9 @@ export default function App() {
212239
}
213240
```
214241
242+
</TabItem>
243+
</Tabs>
244+
215245
## I get the warning "Non-serializable values were found in the navigation state"
216246
217247
This can happen if you are passing non-serializable values such as class instances, functions etc. in params. React Navigation warns you in this case because this can break other functionality such [state persistence](state-persistence.md), [deep linking](deep-linking.md) etc.
@@ -239,6 +269,25 @@ LogBox.ignoreLogs([
239269
240270
This can happen when you pass a React component to an option that accepts a function returning a react element. For example, the [`headerTitle` option in native stack navigator](native-stack-navigator.md#headerTitle) expects a function returning a react element:
241271
272+
<Tabs groupId="config" queryString="config">
273+
<TabItem value="static" label="Static" default>
274+
275+
```js
276+
const Stack = createNativeStackNavigator({
277+
screens: {
278+
Home: {
279+
screen: Home,
280+
options: {
281+
headerTitle: (props) => <MyTitle {...props} />,
282+
},
283+
},
284+
},
285+
});
286+
```
287+
288+
</TabItem>
289+
<TabItem value="dynamic" label="Dynamic" default>
290+
242291
```js
243292
<Stack.Screen
244293
name="Home"
@@ -247,8 +296,31 @@ This can happen when you pass a React component to an option that accepts a func
247296
/>
248297
```
249298
299+
</TabItem>
300+
</Tabs>
301+
250302
If you directly pass a function here, you'll get this error when using hooks:
251303
304+
<Tabs groupId="config" queryString="config">
305+
<TabItem value="static" label="Static" default>
306+
307+
```js
308+
const Stack = createNativeStackNavigator({
309+
screens: {
310+
Home: {
311+
screen: Home,
312+
options: {
313+
// This is not correct
314+
headerTitle: MyTitle,
315+
},
316+
},
317+
},
318+
});
319+
```
320+
321+
</TabItem>
322+
<TabItem value="dynamic" label="Dynamic" default>
323+
252324
```js
253325
<Stack.Screen
254326
name="Home"
@@ -260,6 +332,9 @@ If you directly pass a function here, you'll get this error when using hooks:
260332
/>
261333
```
262334
335+
</TabItem>
336+
</Tabs>
337+
263338
The same applies to other options like `headerLeft`, `headerRight`, `tabBarIcon` etc. as well as props such as `tabBar`, `drawerContent` etc.
264339
265340
## Screens are unmounting/remounting during navigation
@@ -268,6 +343,28 @@ Sometimes you might have noticed that your screens unmount/remount, or your loca
268343
269344
The simplest example is something like following:
270345
346+
<Tabs groupId="config" queryString="config">
347+
<TabItem value="static" label="Static" default>
348+
349+
```js
350+
const RootStack = createNativeStackNavigator({
351+
screens: {
352+
Home: () => {
353+
return <SomeComponent />;
354+
},
355+
},
356+
});
357+
358+
const Navigation = createStaticNavigation(RootStack);
359+
360+
function App() {
361+
return <Navigation />;
362+
}
363+
```
364+
365+
</TabItem>
366+
<TabItem value="dynamic" label="Dynamic" default>
367+
271368
```js
272369
function App() {
273370
return (
@@ -283,12 +380,39 @@ function App() {
283380
}
284381
```
285382
383+
</TabItem>
384+
</Tabs>
385+
286386
The `component` prop expects a React Component, but in the example, it's getting a function returning an React Element. While superficially a component and a function returning a React Element look the exact same, they don't behave the same way when used.
287387
288388
Here, every time the component re-renders, a new function will be created and passed to the `component` prop. React will see a new component and unmount the previous component before rendering the new one. This will cause any local state in the old component to be lost. React Navigation will detect and warn for this specific case but there can be other ways you might be creating components during render which it can't detect.
289389
290390
Another easy to identify example of this is when you create a component inside another component:
291391
392+
<Tabs groupId="config" queryString="config">
393+
<TabItem value="static" label="Static" default>
394+
395+
```js
396+
function App() {
397+
const Home = () => {
398+
return <SomeComponent />;
399+
};
400+
401+
const RootStack = createNativeStackNavigator({
402+
screens: {
403+
Home: Home,
404+
},
405+
});
406+
407+
const Navigation = createStaticNavigation(RootStack);
408+
409+
return <Navigation />;
410+
}
411+
```
412+
413+
</TabItem>
414+
<TabItem value="dynamic" label="Dynamic" default>
415+
292416
```js
293417
function App() {
294418
const Home = () => {
@@ -303,8 +427,35 @@ function App() {
303427
}
304428
```
305429
430+
</TabItem>
431+
</Tabs>
432+
306433
Or when you use a higher order component (such as `connect` from Redux, or `withX` functions that accept a component) inside another component:
307434
435+
<Tabs groupId="config" queryString="config">
436+
<TabItem value="static" label="Static" default>
437+
438+
```js
439+
function App() {
440+
const Home = () => {
441+
return <SomeComponent />;
442+
};
443+
444+
const RootStack = createNativeStackNavigator({
445+
screens: {
446+
Home: withSomeData(Home),
447+
},
448+
});
449+
450+
const Navigation = createStaticNavigation(RootStack);
451+
452+
return <Navigation />;
453+
}
454+
```
455+
456+
</TabItem>
457+
<TabItem value="dynamic" label="Dynamic" default>
458+
308459
```js
309460
function App() {
310461
return (
@@ -315,7 +466,33 @@ function App() {
315466
}
316467
```
317468
469+
</TabItem>
470+
</Tabs>
471+
318472
If you're unsure, it's always best to make sure that the components you are using as screens are defined outside of a React component. They could be defined in another file and imported, or defined at the top level scope in the same file:
473+
<Tabs groupId="config" queryString="config">
474+
<TabItem value="static" label="Static" default>
475+
476+
```js
477+
const Home = () => {
478+
return <SomeComponent />;
479+
};
480+
481+
const RootStack = createNativeStackNavigator({
482+
screens: {
483+
Home: Home,
484+
},
485+
});
486+
487+
const Navigation = createStaticNavigation(RootStack);
488+
489+
function App() {
490+
return <Navigation />;
491+
}
492+
```
493+
494+
</TabItem>
495+
<TabItem value="dynamic" label="Dynamic" default>
319496
320497
```js
321498
const Home = () => {
@@ -331,6 +508,9 @@ function App() {
331508
}
332509
```
333510
511+
</TabItem>
512+
</Tabs>
513+
334514
This is not React Navigation specific, but related to React in general. You should always avoid creating components during render, whether you are using React Navigation or not.
335515
336516
## App is not working properly when connected to Chrome Debugger

0 commit comments

Comments
 (0)