Add Callbacks in Beacon
In the @beacon.li/bar
package, you can add callbacks to execute custom logic whenever a specific event occur. These callbacks allow you to extend and customize the behavior of BeaconBar.
Adding a Callback
To add a callback, you can use the Beacon.addCallback
method. This method takes two parameters:
key
(string): The key of the callback. This key should be unique to identify the callback.callback
(function): The callback function to be executed when the event occurs. The callback function signature should be(userId, metaData) => {}
, whereuserId
andmetaData
are optional parameters.
Here's how to use it:
In the example above, we've added a callback with the key 'your_callback_key'
. This callback will execute your custom logic when triggered.
Parameters
key
(string): A unique identifier for the callback. Adding a callback with the same key as an existing one will replace the old callback.callback
(function): The callback function to execute. It receives two optional parameters:userId
(string, default:beacon-anonymous
): The unique identifier passed duringloadUser
.metaData
(object, default:{}
): ThemetaData
object passed duringloadUser
.
Replacing Callbacks
It's important to note that adding a callback with the same key as an existing one will replace the old callback. This allows you to update callback logic dynamically when needed.
Use Cases
Callbacks can be useful for various scenarios, such as:
Responding to specific user interactions within BeaconBar.
Customizing the behavior of beacon based on user data or actions.
Integrating with external systems or services when certain events occur.
By following these steps, you can add custom callbacks to enhance the functionality and customization of BeaconBar in your application.
Last updated