Skip to content

Commit

Permalink
Make the browser back button work (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
duogenesis authored Oct 29, 2023
1 parent cc84099 commit fa6a540
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import {
DefaultTheme,
NavigationContainer,
NavigationContainerRef,
} from '@react-navigation/native';

import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
Expand Down Expand Up @@ -139,6 +140,7 @@ const App = () => {
const [serverStatus, setServerStatus] = useState<ServerStatus>("ok");
[signedInUser, setSignedInUser] = useState<SignedInUser | undefined>();
[referrerId, setReferrerId] = useState<string | undefined>();
const navigationContainerRef = useRef<any>();

const loadFonts = useCallback(async () => {
await Font.loadAsync({
Expand Down Expand Up @@ -266,6 +268,26 @@ const App = () => {
})();
}, [signedInUser?.personId, signedInUser?.sessionToken]);

const pushBrowserState = Platform.OS !== 'web' ? undefined : useCallback(() => {
history.pushState((history?.state ?? 0) + 1, "", "#");
}, []);

if (Platform.OS === 'web') {
useEffect(() => {
const handlePopstate = (ev) => {
ev.preventDefault();

const navigationContainer = navigationContainerRef?.current;

if (navigationContainer) {
navigationContainer.goBack();
}
};

window.addEventListener('popstate', handlePopstate);
}, []);
}

const onLayoutRootView = useCallback(async () => {
if (!isLoading) {
await SplashScreen.hideAsync();
Expand All @@ -280,6 +302,8 @@ const App = () => {
<>
{!isLoading &&
<NavigationContainer
ref={navigationContainerRef}
onStateChange={pushBrowserState}
theme={{
...DefaultTheme,
colors: {
Expand Down

0 comments on commit fa6a540

Please sign in to comment.