Skip to content

Commit 30ba63d

Browse files
authored
docs: update screen tracking for React Navigation (#553)
Currently, the documentation for screen tracking in React Navigation requires manually implementing a utility to determine the focused screen and can also result in `Unknown` screen. This updates the documentation to follow the official React Navigation docs to use the built-in methods instead. The example app also needs to be updated, but currently, I don't have time to do this. Note: I didn't use `useNavigationRef` to stay compatible with React Navigation 5. Docs: https://reactnavigation.org/docs/screen-tracking
1 parent 1f960f0 commit 30ba63d

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

README.md

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -372,42 +372,28 @@ Our [example app](./example) is set up with screen tracking using React Navigati
372372

373373
Essentially what we'll do is find the root level navigation container and call `screen()` whenever user has navigated to a new screen.
374374

375-
Find the file where you've used the `NavigationContainer` - the main top level container for React Navigation. In this component, create a new state variable to store the current route name:
375+
Find the file where you've used the `NavigationContainer` - the main top level container for React Navigation. In this component, create 2 new refs to store the `navigation` object and the current route name:
376376

377377
```js
378-
const [routeName, setRouteName] = useState('Unknown');
378+
const navigationRef = useRef(null);
379+
const routeNameRef = useRef(null);
379380
```
380381

381-
Now, outside of the component, create a utility function for determining the name of the selected route:
382-
383-
```js
384-
const getActiveRouteName = (
385-
state: NavigationState | PartialState<NavigationState> | undefined
386-
): string => {
387-
if (!state || typeof state.index !== 'number') {
388-
return 'Unknown';
389-
}
390-
391-
const route = state.routes[state.index];
392-
393-
if (route.state) {
394-
return getActiveRouteName(route.state);
395-
}
396-
397-
return route.name;
398-
};
399-
```
400-
401-
Finally, pass a function in the `onStateChange` prop of your `NavigationContainer` that checks for the active route name and calls `client.screen()` if the route has changes. You can pass in any additional screen parameters as the second argument for screen call as needed.
382+
Next, pass the ref to `NavigationContainer` and a function in the `onReady` prop to store the initial route name. Finally, pass a function in the `onStateChange` prop of your `NavigationContainer` that checks for the active route name and calls `client.screen()` if the route has changes. You can pass in any additional screen parameters as the second argument for screen call as needed.
402383

403384
```js
404385
<NavigationContainer
405-
onStateChange={(state) => {
406-
const newRouteName = getActiveRouteName(state);
386+
ref={navigationRef}
387+
onReady={() => {
388+
routeNameRef.current = navigationRef.current.getCurrentRoute().name;
389+
}}
390+
onStateChange={() => {
391+
const previousRouteName = routeNameRef.current;
392+
const currentRouteName = navigationRef.current?.getCurrentRoute().name;
407393

408-
if (routeName !== newRouteName) {
409-
segmentClient.screen(newRouteName);
410-
setRouteName(newRouteName);
394+
if (previousRouteName !== currentRouteName) {
395+
segmentClient.screen(currentRouteName);
396+
routeNameRef.current = currentRouteName;
411397
}
412398
}}
413399
>

0 commit comments

Comments
 (0)