Skip to content

Commit

Permalink
Fix performance issues on navigation changes from post feeds (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored Aug 13, 2023
1 parent eb78292 commit addf95d
Show file tree
Hide file tree
Showing 21 changed files with 615 additions and 562 deletions.
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

0 comments on commit addf95d

Please sign in to comment.