echo
echo copied to clipboard
[Feature Request] React: Provide event name in useEcho callback
Currently, the useEcho hook allows subscribing to multiple events with a single callback:
const { listen } = useEcho(
'channel',
["UserCreated", "UserUpdated"],
(payload) => {
console.log(payload);
}
);
While functional, this makes it impossible to determine which specific event was triggered, especially when handling multiple events that share similar payload structures.
This PR enhances the useEcho hook by passing the event name as a second argument to the callback:
const { listen } = useEcho(
'channel',
["UserCreated", "UserUpdated"],
(payload, event) => {
// `event` will be "UserCreated" or "UserUpdated"
console.log(payload, event);
}
);
Benefits to end users Enables precise event handling when listening to multiple events.
Reduces the need for workarounds such as including the event name in the payload manually.
Backward compatibility The change is backward-compatible: existing usage with a single payload parameter remains functional.
Additional data (event) is non-breaking and optional for the callback.
NOTE: I had to adapt the tests since the registered function is now anonymous. Verifying that the original mockCallback was passed directly no longer works as before. I'm not entirely sure how you'd prefer this to be tested—if you have a specific approach in mind, I'm happy to implement it accordingly. I can also add support for this feature in Vue if you think this is something you would accept as a feature.