Skip to content

refactor: Use generic type for usePrevious hook#7011

Open
tassoevan wants to merge 1 commit intodevelopfrom
refactor/type-safe-use-previous
Open

refactor: Use generic type for usePrevious hook#7011
tassoevan wants to merge 1 commit intodevelopfrom
refactor/type-safe-use-previous

Conversation

@tassoevan
Copy link
Contributor

@tassoevan tassoevan commented Feb 26, 2026

Proposed changes

Small intervention on type safety by making usePrevious a generic (React hook) function.

Issue(s)

How to test or reproduce

Screenshots

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • Improvement (non-breaking change which improves a current function)
  • New feature (non-breaking change which adds functionality)
  • Documentation update (if none of the other choices apply)

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works (if applicable)
  • I have added necessary documentation (if applicable)
  • Any dependent changes have been merged and published in downstream modules

Further comments

Summary by CodeRabbit

  • Refactor
    • Internal code improvements for enhanced reliability and type safety.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 26, 2026

Walkthrough

The usePrevious hook was refactored to use TypeScript generics for improved type safety, renamed its parameter from value to state, changed the ref initialization from useRef(value) to useRef<T>(undefined), and removed the dependency array from the effect.

Changes

Cohort / File(s) Summary
usePrevious Hook Refactoring
app/lib/hooks/usePrevious.ts
Converted to generic function with typed ref initialization. Changed parameter name from value to state, updated return type to T | undefined, and removed dependency array from useEffect (now runs on every render instead of tracking [value]).
Dependencies
package.json
Updated package dependencies with 4 additions and 4 removals.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

type: chore, community

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: converting the usePrevious hook to use generics for type safety, which aligns with the core refactoring work shown in the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 566e100 and ade70fc.

📒 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 usePrevious pattern:

  1. First render returns undefined — semantically correct, as there is no "previous" value on initial render.
  2. Effect runs every render — ensures ref.current always holds the prior render's value, avoiding stale values when the tracked state doesn't change between renders.

The return type T | undefined accurately reflects this behavior. The only usage in the codebase (prevAction in ComposerInput.tsx) is fully compatible—it already handles the undefined case via its equality check.

@tassoevan tassoevan requested a review from OtavioStasiak March 2, 2026 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant