Skip to content

Commit

Permalink
fix: isTransitioning onLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Dec 7, 2023
1 parent f655adc commit b886bdb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
13 changes: 8 additions & 5 deletions examples/react/deferred-data/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type CommentType = {

const fetchPosts = async () => {
console.log('Fetching posts...')
await new Promise((r) => setTimeout(r, 500))
await new Promise((r) => setTimeout(r, 100))
return axios
.get<PostType[]>('https://jsonplaceholder.typicode.com/posts')
.then((r) => r.data.slice(0, 10))
Expand All @@ -41,17 +41,20 @@ const fetchPosts = async () => {
const fetchPost = async (postId: string) => {
console.log(`Fetching post with id ${postId}...`)

const commentsPromise = new Promise((r) => setTimeout(r, 1500))
const commentsPromise = new Promise((r) => setTimeout(r, 2000))
.then(() =>
axios.get<CommentType[]>(
`https://jsonplaceholder.typicode.com/comments?postId=${postId}`,
),
)
.then((r) => r.data)

await new Promise((r) => setTimeout(r, 150))
const post = await axios
.get<PostType>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
const post = await new Promise((r) => setTimeout(r, 1000))
.then(() =>
axios.get<PostType>(
`https://jsonplaceholder.typicode.com/posts/${postId}`,
),
)
.catch((err) => {
if (err.response.status === 404) {
throw new NotFoundError(`Post with id "${postId}" not found!`)
Expand Down
10 changes: 8 additions & 2 deletions packages/react-router/src/RouterProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ function Transitioner() {

useLayoutEffect(() => {
if (
routerState.isTransitioning &&
!isTransitioning &&
!routerState.isLoading &&
routerState.resolvedLocation !== routerState.location
) {
console.log('onResolved', routerState.location)
router.emit({
type: 'onResolved',
fromLocation: routerState.resolvedLocation,
Expand All @@ -185,7 +185,13 @@ function Transitioner() {
resolvedLocation: s.location,
}))
}
}, [isTransitioning, routerState.isLoading])
}, [
routerState.isTransitioning,
isTransitioning,
routerState.isLoading,
routerState.resolvedLocation,
routerState.location,
])

useLayoutEffect(() => {
if (!window.__TSR_DEHYDRATED__) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ export function getInitialRouterState(
isLoading: false,
isTransitioning: false,
status: 'idle',
resolvedLocation: location,
resolvedLocation: { ...location },
location,
matches: [],
pendingMatches: [],
Expand Down

0 comments on commit b886bdb

Please sign in to comment.