Skip to content

Commit

Permalink
refactor: update useRouteParams hook to prevent unnecessary re-renders (
Browse files Browse the repository at this point in the history
#1872)

* refactor: deprecate useRouteParams hook and replace with useRouteParamsSelector to prevent unnecessary re-renders

* chore: clean code
  • Loading branch information
lawvs authored Nov 27, 2024
1 parent 742f915 commit 970aced
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
7 changes: 3 additions & 4 deletions apps/renderer/src/hooks/biz/useRouteParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ const parseRouteParams = (params: Params<any>, search: URLSearchParams): BizRout
}

export const useRouteParams = () => {
const params = useParams()
const [search] = useSearchParams()

return parseRouteParams(params, search)
return useRouteParamsSelector((s) => s)
}

const noop = [] as any[]

export const useRouteParamsSelector = <T>(
selector: (params: BizRouteParams) => T,
deps = noop,
Expand Down
5 changes: 2 additions & 3 deletions apps/renderer/src/modules/entry-column/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ export const useEntriesByView = ({
onReset?: () => void
isArchived?: boolean
}) => {
const routeParams = useRouteParams()
const unreadOnly = useGeneralSettingKey("unreadOnly")
const { feedId, isAllFeeds, view, isCollection, inboxId, listId } = useRouteParams()

const { feedId, view, isAllFeeds, isCollection, listId, inboxId } = routeParams
const unreadOnly = useGeneralSettingKey("unreadOnly")

const folderIds = useFolderFeedsByFeedId({
feedId,
Expand Down
1 change: 0 additions & 1 deletion apps/renderer/src/store/feed/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export const useInboxByIdSelector = <T>(

export const useFeedHeaderTitle = () => {
const { t } = useTranslation()

const { feedId: currentFeedId, view, listId, inboxId } = useRouteParams()

const listTitle = useListByIdSelector(listId, getPreferredTitle)
Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/atoms/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { atom, useAtomValue } from "jotai"
import { selectAtom } from "jotai/utils"
import { useMemo } from "react"
import type { Location, NavigateFunction, Params } from "react-router"
import { shallow } from "zustand/shallow"

interface RouteAtom {
params: Readonly<Params<string>>
Expand All @@ -29,7 +30,8 @@ const noop = []
export const useReadonlyRouteSelector = <T>(
selector: (route: RouteAtom) => T,
deps: any[] = noop,
): T => useAtomValue(useMemo(() => selectAtom(routeAtom, (route) => selector(route)), deps))
): T =>
useAtomValue(useMemo(() => selectAtom(routeAtom, (route) => selector(route), shallow), deps))

// Vite HMR will create new router instance, but RouterProvider always stable

Expand Down

0 comments on commit 970aced

Please sign in to comment.