Skip to content

fix(hooks): forward all arguments in useAppEvent for custom events - #341

Open
JonasPfi wants to merge 1 commit into
playcanvas:mainfrom
JonasPfi:fix/use-app-event-custom-args
Open

fix(hooks): forward all arguments in useAppEvent for custom events#341
JonasPfi wants to merge 1 commit into
playcanvas:mainfrom
JonasPfi:fix/use-app-event-custom-args

Conversation

@JonasPfi

Copy link
Copy Markdown

Summary

useAppEvent only forwarded the callback argument for the built-in update event. For every other event (including custom events fired via app.fire(...)) the callback was invoked with no arguments at all.

Root cause

const handler = useCallback(
    (...args: unknown[]) => {
        if (event === 'update') {
            (callback as (dt: number) => void)(args[0] as number);
        } else {
            (callback as () => void)(); // args dropped here
        }
    },
    [callback, event]
);

Only 'update' was special-cased to receive its argument. Every other event was invoked with zero arguments.

Fix

const handler = useCallback(
    (...args: unknown[]) => {
        (callback as (...args: unknown[]) => void)(...args);
    },
    [callback]
);

Forwards all arguments passed to app.fire(...) generically. update, prerender, and postrender continue to work exactly as before, since they simply receive their normal arguments through the same spread.

Testing

Added a new test (should forward all arguments to the callback for custom events) that fires a custom event via app.fire('levelComplete', 3, 1000) and asserts the callback receives both arguments.

I verified this test fails against the old implementation (callback called with no arguments) and passes against the fix, confirming the test actually exercises the bug rather than passing coincidentally.

Note: the existing test file has a comment explaining that built-in input events can't be fired in the null device type used in tests. This doesn't apply here : app.fire(...) is the underlying EventHandler mechanism, not a hardware input source, so it works fine in the test environment.

@changeset-bot

changeset-bot Bot commented Aug 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2ed0d6f

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

This PR includes changesets to release 2 packages
Name Type
@playcanvas/react Patch
@playcanvas/blocks 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

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