Skip to content

Commit f7735e0

Browse files
7418claude
andcommitted
fix: 修复关闭最后一个分屏时 setState-during-render 报错
- removeFromSplit 中 setSplitSessions updater 内直接调用 router.replace 导致在渲染 AppShell 时触发 Router 组件状态更新 - 改为将导航目标存入 useRef,通过 useEffect 延迟执行 router.replace Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7696cbf commit f7735e0

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/components/layout/AppShell.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { useState, useEffect, useCallback, useMemo } from "react";
3+
import { useState, useEffect, useCallback, useMemo, useRef } from "react";
44
import { usePathname, useRouter } from "next/navigation";
55
import { TooltipProvider } from "@/components/ui/tooltip";
66
import { NavRail } from "./NavRail";
@@ -208,14 +208,15 @@ export function AppShell({ children }: { children: React.ReactNode }) {
208208
});
209209
}, [sessionId, sessionTitle, workingDirectory]);
210210

211+
const pendingNavigateRef = useRef<string | null>(null);
212+
211213
const removeFromSplit = useCallback((removeId: string) => {
212214
setSplitSessions((prev) => {
213215
const next = prev.filter((s) => s.sessionId !== removeId);
214216
if (next.length <= 1) {
215-
// Exit split mode
217+
// Exit split mode — defer navigation to useEffect
216218
if (next.length === 1) {
217-
// Navigate to the remaining session
218-
router.replace(`/chat/${next[0].sessionId}`);
219+
pendingNavigateRef.current = next[0].sessionId;
219220
}
220221
return [];
221222
}
@@ -225,7 +226,16 @@ export function AppShell({ children }: { children: React.ReactNode }) {
225226
);
226227
return next;
227228
});
228-
}, [router]);
229+
}, []);
230+
231+
// Deferred navigation after split exit (avoids setState-during-render)
232+
useEffect(() => {
233+
if (pendingNavigateRef.current) {
234+
const target = pendingNavigateRef.current;
235+
pendingNavigateRef.current = null;
236+
router.replace(`/chat/${target}`);
237+
}
238+
}, [splitSessions, router]);
229239

230240
const exitSplit = useCallback(() => {
231241
const firstSession = splitSessions[0];

0 commit comments

Comments
 (0)