Skip to content

Commit

Permalink
fix: use React.startTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Nov 16, 2022
1 parent 2294802 commit 8dc36ec
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/react/basic-ssr/src/routeConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const routeConfig = createRouteConfig().createChildren((createRoute) => [
component: Posts,
errorComponent: () => 'Oh crap!',
loader: async () => {
await new Promise((resolve) => setTimeout(resolve, 2000))
await new Promise((resolve) => setTimeout(resolve, 500))
return {
posts: await fetchPosts(),
}
Expand All @@ -30,7 +30,7 @@ export const routeConfig = createRouteConfig().createChildren((createRoute) => [
path: ':postId',
component: Post,
loader: async ({ params: { postId } }) => {
await new Promise((resolve) => setTimeout(resolve, 4000))
await new Promise((resolve) => setTimeout(resolve, 300))
return {
post: await fetchPostById(postId),
}
Expand Down
4 changes: 0 additions & 4 deletions packages/react-router-devtools/src/devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,6 @@ export const TanStackRouterDevtoolsPanel = React.forwardRef<
<td style={{ opacity: '.5' }}>Status</td>
<td>{activeMatch.status}</td>
</tr>
<tr>
<td style={{ opacity: '.5' }}>Pending</td>
<td>{activeMatch.isPending.toString()}</td>
</tr>
<tr>
<td style={{ opacity: '.5' }}>Invalid</td>
<td>{activeMatch.isInvalid.toString()}</td>
Expand Down
12 changes: 9 additions & 3 deletions packages/react-router/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,12 @@ export function createReactRouter<
next,
} = linkInfo

const reactHandleClick = (e: Event) => {
React.startTransition(() => {
handleClick(e)
})
}

const composeHandlers =
(handlers: (undefined | ((e: any) => void))[]) =>
(e: React.SyntheticEvent) => {
Expand All @@ -292,7 +298,7 @@ export function createReactRouter<
...resolvedInactiveProps,
...rest,
href: disabled ? undefined : next.href,
onClick: composeHandlers([handleClick, onClick]),
onClick: composeHandlers([reactHandleClick, onClick]),
onFocus: composeHandlers([handleFocus, onFocus]),
onMouseEnter: composeHandlers([handleEnter, onMouseEnter]),
onMouseLeave: composeHandlers([handleLeave, onMouseLeave]),
Expand Down Expand Up @@ -472,12 +478,12 @@ export function Outlet() {
const matches = useMatches().slice(1)
const match = matches[0]

const defaultPending = React.useCallback(() => null, [])

if (!match) {
return null
}

const defaultPending = React.useCallback(() => null, [])

const PendingComponent = (match.__.pendingComponent ??
router.options.defaultPendingComponent ??
defaultPending) as any
Expand Down

0 comments on commit 8dc36ec

Please sign in to comment.