-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix performance issues on navigation changes from post feeds (#649)
- Loading branch information
Showing
21 changed files
with
615 additions
and
562 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
Oops, something went wrong.