Skip to content

Commit 0aacd7d

Browse files
committed
Fix page keeping state after navigating away.
This is to handle nexjts stupid assumption with cacheComponents that all pages are wrapped in <Activity> which maintains state across navigations See: https://nextjs.org/docs/app/getting-started/cache-components#navigation-uses-activity vercel/next.js#85502 vercel/next.js#86577
1 parent 68e3def commit 0aacd7d

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/app/page.client.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { useRouter } from "next/navigation";
4-
import { type FunctionComponent, useState } from "react";
4+
import { type FunctionComponent, useEffect, useState } from "react";
55
import { useBoolean } from "react-use";
66
import Stack from "^/components/ui/Stack";
77
import { type AutocompleteSuggestion } from "^/lib/autocomplete";
@@ -76,4 +76,27 @@ const IndexPageClient: FunctionComponent<LandingProps> = ({
7676
);
7777
};
7878

79-
export default IndexPageClient;
79+
// Wrapper to reset state on unmount
80+
// This is to handle nexjts stupid assumption with cacheComponents
81+
// that all pages are wrapped in <Activity> which maintains state across navigations
82+
// See:
83+
// https://nextjs.org/docs/app/getting-started/cache-components#navigation-uses-activity
84+
// https://github.com/vercel/next.js/discussions/85502
85+
// https://github.com/vercel/next.js/issues/86577
86+
const IndexPageClientWrapper: FunctionComponent<LandingProps> = ({
87+
fallbackSuggestions,
88+
}) => {
89+
const [key, setKey] = useState(0);
90+
91+
useEffect(() => {
92+
return function onUnmount() {
93+
setKey((prev) => prev + 1);
94+
};
95+
}, []);
96+
97+
return (
98+
<IndexPageClient fallbackSuggestions={fallbackSuggestions} key={key} />
99+
);
100+
};
101+
102+
export default IndexPageClientWrapper;

0 commit comments

Comments
 (0)