Skip to content

Commit acb9aa6

Browse files
committed
fix: prevent state updates after component unmount in canvas editor
- Add isMountedRef to track component mount status - Add cleanup effect that sets isMountedRef to false on unmount - Guard all useEffect state updates with isMountedRef check - Prevents 'signal is aborted without reason' errors from pending requests This resolves AbortError warnings when navigating away from the canvas page while the agent is still streaming or processing.
1 parent 4d7eb0e commit acb9aa6

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

apps/app/src/app/canvas/page.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ const DocumentEditor = () => {
7171
const [placeholderVisible, setPlaceholderVisible] = useState(false);
7272
const [currentDocument, setCurrentDocument] = useState("");
7373
const wasRunning = useRef(false);
74+
const isMountedRef = useRef(true);
75+
76+
// Cleanup on unmount to prevent state updates after component is removed
77+
useEffect(() => {
78+
return () => {
79+
isMountedRef.current = false;
80+
};
81+
}, []);
7482

7583
useConfigureSuggestions({
7684
suggestions: [
@@ -113,6 +121,8 @@ const DocumentEditor = () => {
113121

114122
// Handle final state update when run completes
115123
useEffect(() => {
124+
if (!isMountedRef.current) return;
125+
116126
if (wasRunning.current && !isLoading) {
117127
if (currentDocument.trim().length > 0 && currentDocument !== agentState?.document) {
118128
const newDocument = agentState?.document || "";
@@ -143,6 +153,8 @@ const DocumentEditor = () => {
143153

144154
// Sync user edits to agent state
145155
useEffect(() => {
156+
if (!isMountedRef.current) return;
157+
146158
setPlaceholderVisible(text.length === 0);
147159

148160
if (!isLoading && text !== currentDocument) {

0 commit comments

Comments
 (0)