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:

  1. key (string): The key of the callback. This key should be unique to identify the callback.

  2. callback (function): The callback function to be executed when the event occurs. The callback function signature should be (userId, metaData) => {}, where userId and metaData are optional parameters.

Here's how to use it:

import { Beacon } from '@beacon.li/bar';
Beacon.addCallback('your_callback_key', (userId, metaData) => {
    // Your custom logic here
});

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 during loadUser.

    • metaData (object, default: {}): The metaData object passed during loadUser.

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