Skip to content

Commit

Permalink
fix(react-router): fix re-rendering when loaderDeps change (#2752)
Browse files Browse the repository at this point in the history
* fix(react-router): correct cause for loader

see #2749

* stable loaderdeps and search

* allow narrowing on the router state matches

* structural sharing for params, search and loader deps

* avoid re-rendering if matchId changes although we don't care for the matchId
  • Loading branch information
schiller-manuel authored Nov 14, 2024
1 parent ee0a9fc commit ee74d19
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
5 changes: 5 additions & 0 deletions packages/react-router/src/matchContext.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import * as React from 'react'

export const matchContext = React.createContext<string | undefined>(undefined)

// N.B. this only exists so we can conditionally call useContext on it when we are not interested in the nearest match
export const dummyMatchContext = React.createContext<string | undefined>(
undefined,
)
34 changes: 22 additions & 12 deletions packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ export interface RouterErrorSerializer<TSerializedError> {

export interface RouterState<
TRouteTree extends AnyRoute = AnyRoute,
TRouteMatch = MakeRouteMatch<TRouteTree>,
TRouteMatch = MakeRouteMatchUnion,
> {
status: 'pending' | 'idle'
loadedAt: number
Expand Down Expand Up @@ -1242,17 +1242,24 @@ export class Router<
// pending matches that are still loading
const existingMatch = this.getMatch(matchId)

const cause = this.state.matches.find((d) => d.id === matchId)
? 'stay'
: 'enter'
const previousMatch = this.state.matches.find(
(d) => d.routeId === route.id,
)

const cause = previousMatch ? 'stay' : 'enter'

let match: AnyRouteMatch

if (existingMatch) {
match = {
...existingMatch,
cause,
params: routeParams,
params: previousMatch
? replaceEqualDeep(previousMatch.params, routeParams)
: routeParams,
search: previousMatch
? replaceEqualDeep(previousMatch.search, preMatchSearch)
: replaceEqualDeep(existingMatch.search, preMatchSearch),
}
} else {
const status =
Expand All @@ -1267,10 +1274,14 @@ export class Router<
id: matchId,
index,
routeId: route.id,
params: routeParams,
params: previousMatch
? replaceEqualDeep(previousMatch.params, routeParams)
: routeParams,
pathname: joinPaths([this.basepath, interpolatedPath]),
updatedAt: Date.now(),
search: {} as any,
search: previousMatch
? replaceEqualDeep(previousMatch.search, preMatchSearch)
: preMatchSearch,
searchError: undefined,
status,
isFetching: false,
Expand All @@ -1282,7 +1293,9 @@ export class Router<
abortController: new AbortController(),
fetchCount: 0,
cause,
loaderDeps,
loaderDeps: previousMatch
? replaceEqualDeep(previousMatch.loaderDeps, loaderDeps)
: loaderDeps,
invalid: false,
preload: false,
links: route.options.links?.(),
Expand Down Expand Up @@ -1314,10 +1327,7 @@ export class Router<
match.globalNotFound = globalNotFoundRouteId === route.id
}

// Regardless of whether we're reusing an existing match or creating
// a new one, we need to update the match's search params
match.search = replaceEqualDeep(match.search, preMatchSearch)
// And also update the searchError if there is one
// update the searchError if there is one
match.searchError = searchError

const parentMatchId = parentMatch?.id
Expand Down
6 changes: 4 additions & 2 deletions packages/react-router/src/useMatch.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import invariant from 'tiny-invariant'
import { useRouterState } from './useRouterState'
import { matchContext } from './matchContext'
import { dummyMatchContext, matchContext } from './matchContext'
import type {
StructuralSharingOption,
ValidateSelected,
Expand Down Expand Up @@ -84,7 +84,9 @@ export function useMatch<
TStructuralSharing
>,
): ThrowOrOptional<UseMatchResult<TRouter, TFrom, TStrict, TSelected>, TThrow> {
const nearestMatchId = React.useContext(matchContext)
const nearestMatchId = React.useContext(
opts.from ? dummyMatchContext : matchContext,
)

const matchSelection = useRouterState({
select: (state: any) => {
Expand Down

0 comments on commit ee74d19

Please sign in to comment.