refactor: Use generic type for usePrevious hook#7011
refactor: Use generic type for usePrevious hook#7011
usePrevious hook#7011Conversation
WalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@app/lib/hooks/usePrevious.ts`:
- Line 4: The ref initialization in the usePrevious hook causes a TypeScript
error because useRef<T>(undefined) doesn't allow undefined for concrete T;
change the ref to explicitly allow undefined by initializing as useRef<T |
undefined>(undefined) or by calling useRef<T | undefined>() so the ref type
becomes T | undefined (update the ref variable declaration named ref inside
usePrevious).
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
app/lib/hooks/usePrevious.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: ESLint and Test / run-eslint-and-test
🔇 Additional comments (1)
app/lib/hooks/usePrevious.ts (1)
3-11: Good type-safety improvement with behaviorally correct changes.The generic refactor is a solid improvement. The implementation now properly handles the standard
usePreviouspattern:
- First render returns
undefined— semantically correct, as there is no "previous" value on initial render.- Effect runs every render — ensures
ref.currentalways holds the prior render's value, avoiding stale values when the tracked state doesn't change between renders.The return type
T | undefinedaccurately reflects this behavior. The only usage in the codebase (prevActioninComposerInput.tsx) is fully compatible—it already handles the undefined case via its equality check.
Proposed changes
Small intervention on type safety by making
usePreviousa generic (React hook) function.Issue(s)
How to test or reproduce
Screenshots
Types of changes
Checklist
Further comments
Summary by CodeRabbit