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

Fix performance issues on navigation changes from post feeds #649

Merged
merged 6 commits into from
Aug 13, 2023
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
13 changes: 8 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { getDeviceMode } from "./features/settings/settingsSlice";
import { SafeArea, SafeAreaInsets } from "capacitor-plugin-safe-area";
import { StatusBar } from "@capacitor/status-bar";
import { Keyboard } from "@capacitor/keyboard";
import { TabContextProvider } from "./TabContext";

setupIonicReact({
rippleEffect: false,
Expand Down Expand Up @@ -74,11 +75,13 @@ export default function App() {
<BeforeInstallPromptProvider>
<UpdateContextProvider>
<Router>
<IonApp>
<Auth>
<TabbedRoutes />
</Auth>
</IonApp>
<TabContextProvider>
<IonApp>
<Auth>
<TabbedRoutes />
</Auth>
</IonApp>
</TabContextProvider>
</Router>
</UpdateContextProvider>
</BeforeInstallPromptProvider>
Expand Down
45 changes: 45 additions & 0 deletions src/TabContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React, { createContext, useMemo } from "react";
import { useLocation } from "react-router";

interface ITabContext {
tab: string;
}

export const TabContext = createContext<ITabContext>({
tab: "",
});

/**
* The reason for this, instead of useLocation() in components directly to get tab name,
* is that it does not trigger a rerender on navigation changes.
*/
export function TabContextProvider({
children,
}: {
children: React.ReactNode;
}) {
const location = useLocation();

const tab = location.pathname.split("/")[1];

const memoized = useMemo(
() => (
<TabContextProviderInternals tab={tab}>
{children}
</TabContextProviderInternals>
),
[tab, children]
);

return memoized;
}

function TabContextProviderInternals({
tab,
children,
}: {
tab: string;
children: React.ReactNode;
}) {
return <TabContext.Provider value={{ tab }}>{children}</TabContext.Provider>;
}
Loading