Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Optimize AppNavigator #42532

Merged
merged 13 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/libs/Navigation/AppNavigator/index.native.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, {memo, useContext, useEffect} from 'react';
import {NativeModules} from 'react-native';
import {InitialURLContext} from '@components/InitialURLContextProvider';
import Navigation from '@libs/Navigation/Navigation';

type AppNavigatorProps = {
/** If we have an authToken this is true */
authenticated: boolean;
};

function AppNavigator({authenticated}: AppNavigatorProps) {
const initUrl = useContext(InitialURLContext);

useEffect(() => {
if (!NativeModules.HybridAppModule || !initUrl) {
return;
}

Navigation.isNavigationReady().then(() => {
Navigation.navigate(initUrl);
});
}, [initUrl]);

if (authenticated) {
const AuthScreens = require('./AuthScreens').default;

// These are the protected screens and only accessible when an authToken is present
return <AuthScreens />;
}

const PublicScreens = require('./PublicScreens').default;

return <PublicScreens />;
}

AppNavigator.displayName = 'AppNavigator';

export default memo(AppNavigator);
24 changes: 17 additions & 7 deletions src/libs/Navigation/AppNavigator/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, {useContext, useEffect} from 'react';
import React, {lazy, memo, Suspense, useContext, useEffect} from 'react';
import {NativeModules} from 'react-native';
import {InitialURLContext} from '@components/InitialURLContextProvider';
import Navigation from '@libs/Navigation/Navigation';

const AuthScreens = lazy(() => import('./AuthScreens'));
const PublicScreens = lazy(() => import('./PublicScreens'));

type AppNavigatorProps = {
/** If we have an authToken this is true */
authenticated: boolean;
Expand All @@ -22,14 +25,21 @@ function AppNavigator({authenticated}: AppNavigatorProps) {
}, [initUrl]);

if (authenticated) {
const AuthScreens = require('./AuthScreens').default;

// These are the protected screens and only accessible when an authToken is present
return <AuthScreens />;
return (
<Suspense fallback={null}>
<AuthScreens />
</Suspense>
);
}
const PublicScreens = require('./PublicScreens').default;
return <PublicScreens />;

return (
<Suspense fallback={null}>
<PublicScreens />
</Suspense>
);
}

AppNavigator.displayName = 'AppNavigator';
export default AppNavigator;

export default memo(AppNavigator);
Loading