Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update window.history before setting updated React state on navigations #10448

Merged
merged 3 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/big-vans-act.md
@@ -0,0 +1,6 @@
---
"@remix-run/router": patch
"react-router-dom": patch
---

Fix window.location is not equal to useLocation()
1 change: 1 addition & 0 deletions contributors.yml
Expand Up @@ -215,3 +215,4 @@
- yionr
- yuleicul
- zheng-chuang
- holynewbie
20 changes: 10 additions & 10 deletions packages/router/router.ts
Expand Up @@ -1002,6 +1002,16 @@ export function createRouter(init: RouterInit): Router {
inFlightDataRoutes = undefined;
}

if (isUninterruptedRevalidation) {
// If this was an uninterrupted revalidation then do not touch history
} else if (pendingAction === HistoryAction.Pop) {
// Do nothing for POP - URL has already been updated
} else if (pendingAction === HistoryAction.Push) {
init.history.push(location, location.state);
} else if (pendingAction === HistoryAction.Replace) {
init.history.replace(location, location.state);
}

updateState({
...newState, // matches, errors, fetchers go through as-is
actionData,
Expand All @@ -1019,16 +1029,6 @@ export function createRouter(init: RouterInit): Router {
blockers: new Map(state.blockers),
});

if (isUninterruptedRevalidation) {
// If this was an uninterrupted revalidation then do not touch history
} else if (pendingAction === HistoryAction.Pop) {
// Do nothing for POP - URL has already been updated
} else if (pendingAction === HistoryAction.Push) {
init.history.push(location, location.state);
} else if (pendingAction === HistoryAction.Replace) {
init.history.replace(location, location.state);
}

// Reset stateful navigation vars
pendingAction = HistoryAction.Pop;
pendingPreventScrollReset = false;
Expand Down