Skip to content

fix(react-router): scroll with revalidation #13671

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

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/kind-paws-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

fix page twitching when scrolling with revalidation
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
- decadentsavant
- developit
- dgrijuela
- DimaAmega
- DigitalNaut
- dmitrytarassov
- dokeet
Expand Down
40 changes: 40 additions & 0 deletions packages/react-router/__tests__/router/scroll-restoration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,46 @@ describe("scroll restoration", () => {
expect(t.router.state.restoreScrollPosition).toBe(50);
expect(t.router.state.preventScrollReset).toBe(false);
});

it("does not restore scroll on revalidation", async () => {
let t = setup({
routes: SCROLL_ROUTES,
initialEntries: ["/"],
});

expect(t.router.state.restoreScrollPosition).toBe(null);
expect(t.router.state.preventScrollReset).toBe(false);

let positions = {};

// Simulate scrolling to 100 on /
let activeScrollPosition = 100;
t.router.enableScrollRestoration(positions, () => activeScrollPosition);

// Revalidate
let R = await t.revalidate();
await R.loaders.index.resolve("INDEX");

expect(t.router.state.restoreScrollPosition).toBe(false);
expect(t.router.state.preventScrollReset).toBe(false);

// Scroll to 200
activeScrollPosition = 200;

// Go to /tasks
let nav1 = await t.navigate("/tasks");
await nav1.loaders.tasks.resolve("TASKS");

expect(t.router.state.restoreScrollPosition).toBe(null);
expect(t.router.state.preventScrollReset).toBe(false);

// Restore on pop back to /
let nav2 = await t.navigate(-1);
expect(t.router.state.restoreScrollPosition).toBe(null);
await nav2.loaders.index.resolve("INDEX");
expect(t.router.state.restoreScrollPosition).toBe(200);
expect(t.router.state.preventScrollReset).toBe(false);
});
});

describe("scroll reset", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-router/lib/dom/lib.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@ export function useScrollRestoration({
// Restore scrolling when state.restoreScrollPosition changes
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useLayoutEffect(() => {
// Explicit false means don't do anything (used for submissions)
// Explicit false means don't do anything (used for submissions or revalidations)
if (restoreScrollPosition === false) {
return;
}
Expand Down
10 changes: 6 additions & 4 deletions packages/react-router/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,11 @@ export function createRouter(init: RouterInit): Router {
blockers.forEach((_, k) => blockers.set(k, IDLE_BLOCKER));
}

// Don't restore on revalidation
let restoreScrollPosition =
state.revalidation === "idle" &&
getSavedScrollPosition(location, newState.matches || state.matches);

// Always respect the user flag. Otherwise don't reset on mutation
// submission navigations unless they redirect
let preventScrollReset =
Expand Down Expand Up @@ -1337,10 +1342,7 @@ export function createRouter(init: RouterInit): Router {
initialized: true,
navigation: IDLE_NAVIGATION,
revalidation: "idle",
restoreScrollPosition: getSavedScrollPosition(
location,
newState.matches || state.matches
),
restoreScrollPosition,
preventScrollReset,
blockers,
},
Expand Down