fix(react-router): clear stale error in CatchBoundary on route change#7122
fix(react-router): clear stale error in CatchBoundary on route change#7122ali-idrizi wants to merge 2 commits intoTanStack:mainfrom
Conversation
📝 WalkthroughWalkthroughThe changes refactor error reset handling in Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
View your CI Pipeline Execution ↗ for commit 669f802
☁️ Nx Cloud last updated this comment at |
Bundle Size Benchmarks
Trend sparkline is historical gzip bytes ending with this PR measurement; lower is better. |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/react-router/src/CatchBoundary.tsx (1)
40-46: Use proper types instead ofanyfor type safety.The
getDerivedStateFromPropsmethod usesanyfor bothpropsandstateparameters, which bypasses TypeScript's type checking. Consider using the component's actual prop and state types.♻️ Proposed fix for type safety
- static getDerivedStateFromProps(props: any, state: any) { + static getDerivedStateFromProps( + props: { + getResetKey: () => number | string + children: (props: { error: Error | null; reset: () => void }) => React.ReactNode + onCatch?: (error: Error, errorInfo: ErrorInfo) => void + }, + state: { error: Error | null; resetKey: number | string }, + ) { const resetKey = props.getResetKey() if (state.error && state.resetKey !== resetKey) { return { resetKey: resetKey, error: null } } return { resetKey: resetKey } }As per coding guidelines: "Use TypeScript strict mode with extensive type safety".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react-router/src/CatchBoundary.tsx` around lines 40 - 46, Replace the use of `any` in the static method signature with the component's concrete prop and state types: change `getDerivedStateFromProps(props: any, state: any)` to use the component's Props and State (e.g., `getDerivedStateFromProps(props: CatchBoundaryProps, state: CatchBoundaryState)` or the generic types declared on the `CatchBoundary` class), make the method return a correctly typed Partial<State> or null, and ensure the State interface includes `error` and `resetKey` (the method reads `state.error` and `state.resetKey`) while Props exposes `getResetKey(): string` so `const resetKey = props.getResetKey()` type-checks.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/react-router/src/CatchBoundary.tsx`:
- Around line 40-46: Replace the use of `any` in the static method signature
with the component's concrete prop and state types: change
`getDerivedStateFromProps(props: any, state: any)` to use the component's Props
and State (e.g., `getDerivedStateFromProps(props: CatchBoundaryProps, state:
CatchBoundaryState)` or the generic types declared on the `CatchBoundary`
class), make the method return a correctly typed Partial<State> or null, and
ensure the State interface includes `error` and `resetKey` (the method reads
`state.error` and `state.resetKey`) while Props exposes `getResetKey(): string`
so `const resetKey = props.getResetKey()` type-checks.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: efbe2fab-6225-41b8-bfb0-c14a7b2b6814
📒 Files selected for processing (2)
packages/react-router/src/CatchBoundary.tsxpackages/react-router/tests/link.test.tsx
fixes #7121
I have modified the
CatchBoundaryto reset theerrortogether with theresetKeyingetDerivedStateFromProps. This removes the need forcomponentDidUpdateto callreset(), and makes the renderresetKeymismatch check redundant.An existing test that was testing the exact case in #7121 but for
onErroris slightly modified to also test no calls are made toerrorComponent.Summary by CodeRabbit
Bug Fixes
Tests