Skip to content

fix(react): ensure event callbacks are updated on changes#250

Open
jamsinclair wants to merge 2 commits into
PuruVJ:mainfrom
jamsinclair:fix-stale-callbacks
Open

fix(react): ensure event callbacks are updated on changes#250
jamsinclair wants to merge 2 commits into
PuruVJ:mainfrom
jamsinclair:fix-stale-callbacks

Conversation

@jamsinclair

@jamsinclair jamsinclair commented Sep 15, 2025

Copy link
Copy Markdown

Fixes #249

Problem

The hook does not correctly update the event callbacks passed through in the options. This results in stale references when the callbacks are invoked.

Solution

Update to keep track of the callbacks in refs and call these in the callbacks registered to the core draggable instance.

Update the callback refs to ensure they remain up-to-date in the existing useEffect that has a dependency on options


Question: Will this also need to be fixed in v3?

@changeset-bot

changeset-bot Bot commented Sep 15, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b53a41b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@neodrag/react Patch

Not sure what this means? Click here to learn what changesets are.

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

@vercel

vercel Bot commented Sep 15, 2025

Copy link
Copy Markdown

@jamsinclair is attempting to deploy a commit to the Purus Projects Team on Vercel.

A member of the Team first needs to authorize it.

}, []);

useEffect(() => {
// Update refs with latest options callbacks

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I think this useEffect retriggers on every re-render unless the consumer passes a stable options object reference to the useDraggable hook.

I don't think this will cause any performance concerns. If for some odd reason it does, the consumer can always wrap their object in useMemo or alternative solutions.

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.

Stale event callbacks for the react package.

1 participant