Skip to content

Commit

Permalink
fix: restore router.history and usePrompt behavior (#461)
Browse files Browse the repository at this point in the history
* fix: restore router.history and usePrompt behavior

* fix: revert to router history property
  • Loading branch information
tombuntus committed Dec 22, 2022
1 parent 6f0f747 commit c4ce01b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
21 changes: 10 additions & 11 deletions packages/react-router/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -761,17 +761,16 @@ export function usePrompt(message: string, when: boolean | any): void {
React.useEffect(() => {
if (!when) return

// TODO: bring this back
// let unblock = router.history.block((transition) => {
// if (window.confirm(message)) {
// unblock()
// transition.retry()
// } else {
// router.store.currentLocation.pathname = window.location.pathname
// }
// })

// return unblock
let unblock = router.history.block((transition) => {
if (window.confirm(message)) {
unblock()
transition.retry()
} else {
router.store.currentLocation.pathname = window.location.pathname
}
})

return unblock
}, [when, message])
}

Expand Down
17 changes: 8 additions & 9 deletions packages/router-core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export interface Router<
}

// Public API
// history: BrowserHistory | MemoryHistory | HashHistory
history: BrowserHistory | MemoryHistory | HashHistory
options: PickAsRequired<
RouterOptions<TRouteConfig, TRouterContext>,
'stringifySearch' | 'parseSearch' | 'context'
Expand Down Expand Up @@ -410,7 +410,6 @@ export function createRouter<
>(
userOptions?: RouterOptions<TRouteConfig, TRouterContext>,
): Router<TRouteConfig, TAllRouteInfo, TRouterContext> {
let history = userOptions?.history || createDefaultHistory()

const originalOptions = {
defaultLoaderGcMaxAge: 5 * 60 * 1000,
Expand Down Expand Up @@ -585,13 +584,13 @@ export function createRouter<
nextAction = 'push'
}

const isSameUrl = parseLocation(history.location).href === next.href
const isSameUrl = parseLocation(router.history.location).href === next.href

if (isSameUrl && !next.key) {
nextAction = 'replace'
}

history[nextAction](
router.history[nextAction](
{
pathname: next.pathname,
hash: next.hash,
Expand All @@ -617,7 +616,7 @@ export function createRouter<
types: undefined!,

// public api
// history,
history: userOptions?.history || createDefaultHistory(),
store,
setStore,
options: originalOptions,
Expand Down Expand Up @@ -692,7 +691,7 @@ export function createRouter<
router.load()
}

const unsub = history.listen((event) => {
const unsub = router.history.listen((event) => {
router.load(parseLocation(event.location, store.latestLocation))
})

Expand All @@ -719,13 +718,13 @@ export function createRouter<
},

update: (opts) => {
const newHistory = opts?.history !== history
const newHistory = opts?.history !== router.history
if (!store.latestLocation || newHistory) {
if (opts?.history) {
history = opts.history
router.history = opts.history
}
setStore((s) => {
s.latestLocation = parseLocation(history.location)
s.latestLocation = parseLocation(router.history.location)
s.currentLocation = s.latestLocation
})
}
Expand Down

0 comments on commit c4ce01b

Please sign in to comment.