Skip to content

Commit 356cecc

Browse files
longsizhuoclaude
andcommitted
fix: 修复 ThemeProvider 在 effect 中同步调用 setState 的 ESLint 错误
改用 useState 懒初始化直接读 localStorage, 移除 login/page.tsx 中未使用的 resolveRedirectTarget 函数。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 6792aa5 commit 356cecc

2 files changed

Lines changed: 8 additions & 39 deletions

File tree

app/components/ThemeProvider.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,16 @@ export function ThemeProvider({
2727
storageKey = "vite-ui-theme",
2828
...props
2929
}: ThemeProviderProps) {
30-
// 初始状态始终使用 defaultTheme,保证服务端/客户端一致,
31-
// 避免 localStorage 读取导致水合不匹配。
32-
// layout.tsx 中的内联脚本已确保首屏无闪烁(在 React 水合前设置了 CSS class)。
33-
const [theme, setTheme] = useState<Theme>(defaultTheme);
34-
35-
// 挂载后从 localStorage 读取用户之前保存的主题
36-
useEffect(() => {
30+
// 懒初始化:客户端直接从 localStorage 读取,服务端返回 defaultTheme。
31+
// layout.tsx 中的内联脚本在 React 水合前已设置正确的 CSS class,首屏不会 flash。
32+
const [theme, setTheme] = useState<Theme>(() => {
33+
if (typeof window === "undefined") return defaultTheme;
3734
try {
38-
const stored = localStorage.getItem(storageKey) as Theme | null;
39-
if (stored) {
40-
setTheme(stored);
41-
}
35+
return (localStorage.getItem(storageKey) as Theme) || defaultTheme;
4236
} catch {
43-
// 忽略 localStorage 访问错误
37+
return defaultTheme;
4438
}
45-
}, [storageKey]);
39+
});
4640

4741
useEffect(() => {
4842
const root = window.document.documentElement;

app/login/page.tsx

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
11
import { SignInButton } from "@/app/components/SignInButton";
22

3-
type LoginPageProps = {
4-
searchParams: Promise<{
5-
redirectTo?: string | string[];
6-
callbackUrl?: string | string[];
7-
}>;
8-
};
9-
10-
const FALLBACK_CALLBACK_URL = "/";
11-
12-
const coerceSearchParam = (
13-
value: string | string[] | undefined,
14-
): string | undefined => {
15-
if (!value) return undefined;
16-
return Array.isArray(value) ? value[0] : value;
17-
};
18-
19-
const resolveRedirectTarget = (
20-
params: Awaited<LoginPageProps["searchParams"]>,
21-
): string => {
22-
const redirectTo =
23-
coerceSearchParam(params?.redirectTo) ??
24-
coerceSearchParam(params?.callbackUrl);
25-
return redirectTo || FALLBACK_CALLBACK_URL;
26-
};
27-
28-
export default async function LoginPage({ searchParams: _ }: LoginPageProps) {
3+
export default async function LoginPage() {
294
// GitHub OAuth 登录后固定跳回首页(后端回调带 token),登录后各页面自行处理跳转
305
return (
316
<div className="min-h-screen flex items-center justify-center bg-background">

0 commit comments

Comments
 (0)