Skip to content
This repository was archived by the owner on Oct 2, 2025. It is now read-only.
This repository was archived by the owner on Oct 2, 2025. It is now read-only.

Refactor useOnClerkEvent hook for better stability and API safety #186

@coderabbitai

Description

@coderabbitai

Description

The useOnClerkEvent hook in src/lib/useOnClerkEvent.ts could be improved to:

  1. Stabilize callback - Prevent resubscription on every render by using a ref to store the latest callback
  2. Guard for API presence - Check that clerk.addListener exists before attempting to use it
  3. Pass through original event - Forward the complete event object instead of reconstructing {user, session, client}

Current Issues

  • Hook resubscribes on every render when callback identity changes
  • Assumes clerk.addListener exists without checking
  • Strips event metadata by reconstructing the event object

Proposed Changes

  • Use useRef to stabilize the callback reference
  • Add type guards for clerk and clerk.addListener
  • Pass the original event object directly to the user callback

Context

This issue was identified during code review of PR #184.

References:

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions