Skip to content

fix(frontend): resolve set-state-in-effect in useUnsavedChanges (#1063)#1070

Merged
ericsocrat merged 1 commit intomainfrom
chore/react-compiler-unsaved-changes
May 1, 2026
Merged

fix(frontend): resolve set-state-in-effect in useUnsavedChanges (#1063)#1070
ericsocrat merged 1 commit intomainfrom
chore/react-compiler-unsaved-changes

Conversation

@ericsocrat
Copy link
Copy Markdown
Owner

Phase 2 of #1063 — second of 19 react-hooks/set-state-in-effect violations.

Change

In useUnsavedChanges, the effect that mirrored isDirty into a ref also reset pendingHref whenever the form went from dirty to clean. The setPendingHref(null) call inside the effect is the violation.

Replaced the state-reset half with the canonical adjust-state-during-render pattern, comparing isDirty against a previous-value state. The ref-sync effect is kept (no setState involved).

Behaviour is unchanged: when the form transitions back to clean, any pending navigation is cleared.

Verification

  • eslint src/hooks/use-unsaved-changes.ts — clean
  • vitest run src/hooks/use-unsaved-changes.test.ts — 15/15 pass
  • tsc --noEmit — clean

Progress

Replaces a useEffect that synced isDirty into both a ref and a state reset with the canonical 'adjust state during render' pattern for the state portion. The ref sync remains in its own effect (no setState involved). Behaviour is identical: when the form transitions back to clean, any pending navigation is cleared.
Copilot AI review requested due to automatic review settings May 1, 2026 10:13
@vercel
Copy link
Copy Markdown

vercel Bot commented May 1, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
tryvit Ready Ready Preview, Comment May 1, 2026 10:15am

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 1, 2026

Bundle Size Report

Metric Value
Main baseline 0 KB
This PR 0 KB
Delta +0 KB (+0%)
JS chunks 0
Hard limit 4000 KB

✅ Bundle size is within acceptable limits.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the useUnsavedChanges hook to eliminate a react-compiler/set-state-in-effect lint violation while preserving the existing navigation-guard behavior for dirty forms.

Changes:

  • Removes setPendingHref(null) from the useEffect that mirrors isDirty into a ref.
  • Introduces a render-phase “adjust state during render” pattern using prevIsDirty to clear pendingHref only when transitioning from dirty → clean.
  • Keeps the ref-sync effect so event handlers observe the latest isDirty without capturing stale values.

@ericsocrat ericsocrat merged commit 3379245 into main May 1, 2026
22 checks passed
@ericsocrat ericsocrat deleted the chore/react-compiler-unsaved-changes branch May 1, 2026 10:18
ericsocrat added a commit that referenced this pull request May 1, 2026
…) (#1077)

Phase 2 of #1063 -- resolves 2 of 19 violations (running total: 12/19 across 9 PRs).

Two react-hooks/set-state-in-effect warnings in SearchAutocomplete.tsx resolved by adjusting state during render:

1. Refresh recent-searches list on dropdown open: replaced the show-dep useEffect with a prevShow render-phase tracker. Added a lazy initial-state initializer so the list is hydrated on mount when show is already true (preserves existing test contract).

2. Reset activeIndex when suggestions/query change: replaced the multi-dep useEffect with a composite resetKey + prevResetKey render-phase tracker.

Pattern: https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes

Behaviour unchanged. 32/32 vitest pass, eslint and tsc clean.

Cross-references: #1067, #1069, #1070, #1071, #1072, #1073, #1074, #1075, #1076.

Co-authored-by: ericsocrat <ericsocrat@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants