Skip to content

Commit

Permalink
chore(react@18): remove unstable_batchedUpdates
Browse files Browse the repository at this point in the history
 isn't necessary anymore because batching happens automatically

reactwg/react-18#21 (comment)
  • Loading branch information
virtuoushub committed Jul 7, 2022
1 parent 2841061 commit bc19253
Showing 1 changed file with 34 additions and 42 deletions.
76 changes: 34 additions & 42 deletions packages/router/src/active-route-loader.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React, { useRef, useState, useEffect } from 'react'

import { unstable_batchedUpdates } from 'react-dom'

import { getAnnouncement, getFocus, resetFocus } from './a11yUtils'
import {
ActivePageContextProvider,
Expand Down Expand Up @@ -92,19 +90,17 @@ export const ActiveRouteLoader = ({
// Consumers of the context can show a loading indicator
// to signal to the user that something is happening.
loadingTimeout.current = setTimeout(() => {
unstable_batchedUpdates(() => {
setLoadingState((loadingState) => ({
...loadingState,
[path]: {
page: whileLoadingPage || ArlWhileLoadingNullPage,
specName: '',
state: 'SHOW_LOADING',
location,
},
}))
setRenderedChildren(children)
setRenderedPath(path)
})
setLoadingState((loadingState) => ({
...loadingState,
[path]: {
page: whileLoadingPage || ArlWhileLoadingNullPage,
specName: '',
state: 'SHOW_LOADING',
location,
},
}))
setRenderedChildren(children)
setRenderedPath(path)
}, delay)

// Wait to download and parse the page.
Expand All @@ -117,7 +113,6 @@ export const ActiveRouteLoader = ({
// Only update all state if we're still interested (i.e. we're still
// waiting for the page that just finished loading)
if (isMounted() && name === waitingFor.current) {
unstable_batchedUpdates(() => {
setLoadingState((loadingState) => ({
...loadingState,
[path]: {
Expand All @@ -136,40 +131,37 @@ export const ActiveRouteLoader = ({
setRenderedChildren(children ?? module.default)
setRenderedPath(path)
setPageName(name)
})
}
}

if (spec.name !== waitingFor.current) {
clearLoadingTimeout()
startPageLoadTransition(spec, delay)
} else {
unstable_batchedUpdates(() => {
// Handle navigating to the same page again, but with different path
// params (i.e. new `location` or route params)
setLoadingState((loadingState) => {
// If path is same, fetch the page again
let existingPage = loadingState[path]?.page
// If path is different, try to find the existing page
if (!existingPage) {
const pageState = Object.values(loadingState).find(
(state) => state?.specName === spec.name
)
existingPage = pageState?.page
}
return {
...loadingState,
[path]: {
page: existingPage || ArlNullPage,
specName: spec.name,
state: 'DONE',
location,
},
}
})
setRenderedChildren(children)
setRenderedPath(path)
// Handle navigating to the same page again, but with different path
// params (i.e. new `location` or route params)
setLoadingState((loadingState) => {
// If path is same, fetch the page again
let existingPage = loadingState[path]?.page
// If path is different, try to find the existing page
if (!existingPage) {
const pageState = Object.values(loadingState).find(
(state) => state?.specName === spec.name
)
existingPage = pageState?.page
}
return {
...loadingState,
[path]: {
page: existingPage || ArlNullPage,
specName: spec.name,
state: 'DONE',
location,
},
}
})
setRenderedChildren(children)
setRenderedPath(path)
}

return () => {
Expand Down

0 comments on commit bc19253

Please sign in to comment.