Skip to content

Commit b5fc207

Browse files
vicprzAlmouro
authored andcommitted
✨ feat: add logs
1 parent 0ceea79 commit b5fc207

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import { NavigationContainer as ReactNavigationContainer } from "@react-navigati
1616
import { enableLiveReloadOnScreen } from "@bam.tech/react-navigation-live-reload-on-screen";
1717

1818
const ENABLE_LIVE_RELOAD = __DEV__;
19-
const NavigationContainer = enableLiveReloadOnScreen(ENABLE_LIVE_RELOAD)(
19+
const ENABLE_LIVE_RELOAD_LOGS = true;
20+
const NavigationContainer = enableLiveReloadOnScreen(ENABLE_LIVE_RELOAD, ENABLE_LIVE_RELOAD_LOGS)(
2021
ReactNavigationContainer
2122
);
2223

index.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Theme,
1010
LinkingOptions,
1111
DocumentTitleOptions,
12+
InitialState,
1213
} from "@react-navigation/native";
1314
export const PERSISTENCE_KEY = "NAVIGATION_STATE";
1415

@@ -73,7 +74,7 @@ declare type NavigationContainerType<Params extends {}> = (
7374
) => React.ReactElement;
7475

7576
export const enableLiveReloadOnScreen =
76-
(enable: boolean) =>
77+
(enable: boolean, logs: boolean = true) =>
7778
<Params extends {} = {}>(
7879
NavigationContainerComponent: NavigationContainerType<Params>
7980
) => {
@@ -84,6 +85,18 @@ export const enableLiveReloadOnScreen =
8485
) => {
8586
const { waitForLiveReload, props: liveReloadProps } =
8687
useLiveReloadOnScreen();
88+
89+
React.useEffect(() => {
90+
if (logs && !waitForLiveReload) {
91+
const activeRouteName = getActiveRouteName(liveReloadProps.initialState);
92+
if (activeRouteName) {
93+
console.log("[Live Reload on Screen] App reloaded on", activeRouteName);
94+
} else {
95+
console.log("[Live Reload on Screen] No reload, either because the app was opened with a deep link or because this is the first time the app has been launched");
96+
}
97+
}
98+
}, [waitForLiveReload]);
99+
87100
if (waitForLiveReload) return null;
88101

89102
const onStateChange = (state: NavigationState | undefined) => {
@@ -113,3 +126,19 @@ export const enableLiveReloadOnScreen =
113126

114127
export const clearNavigationState = (): Promise<void> =>
115128
AsyncStorage.removeItem(PERSISTENCE_KEY);
129+
130+
const getActiveRouteName = (navigationState?: InitialState): string | null => {
131+
if (!navigationState) return null;
132+
133+
const { index, routes } = navigationState;
134+
if (index === undefined || index >= routes.length) return null;
135+
136+
const activeRoute = routes[index];
137+
138+
// If the active route has a nested state, recurse
139+
if (activeRoute.state) {
140+
return getActiveRouteName(activeRoute.state);
141+
}
142+
143+
return activeRoute.name;
144+
};

0 commit comments

Comments
 (0)