Skip to content

refactor: prevent useExternalEvents effect from running on every render#218

Open
eunwoo-levi wants to merge 2 commits intotoss:mainfrom
eunwoo-levi:refactor/stabilize-use-external-events
Open

refactor: prevent useExternalEvents effect from running on every render#218
eunwoo-levi wants to merge 2 commits intotoss:mainfrom
eunwoo-levi:refactor/stabilize-use-external-events

Conversation

@eunwoo-levi
Copy link
Copy Markdown

@eunwoo-levi eunwoo-levi commented Feb 11, 2026

Description

The handlers object inside useExternalEvents is recreated via Object.entries().reduce() on every render. Since it is used as a dependency of useLayoutEffect, the effect runs on every render, causing unnecessary emitter.off/emitter.on cycles.

Changes

  • Store events in a useRef to always reference the latest callbacks without triggering re-registration.
  • Register stable handler functions on mount only (dependency: []), which delegate to eventsRef.current internally.
  • Clean up listeners on unmount.

Motivation and Context

The previous implementation caused emitter.offemitter.on on every render, which is wasteful and creates a brief window where events could be missed between unsubscribe and resubscribe.

How Has This Been Tested?

  • Verified existing unit tests pass locally (36/36).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have performed a self-review of my own code.
  • My code is commented, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • Any dependent changes have been merged and published in downstream modules.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Feb 11, 2026

⚠️ No Changeset found

Latest commit: 762a7dd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link
Copy Markdown

vercel bot commented Feb 11, 2026

@eunwoo-levi is attempting to deploy a commit to the Toss Team on Vercel.

A member of the Team first needs to authorize it.

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.

1 participant