Widget Events

This guide explains how to integrate and use these events in your application.

import Blockend, { blockendEventsConstants, useBlockendEvents } from "@blockend/widget";

Event System Overview

The widget uses mitt as the underlying event emitter, providing a lightweight and efficient event system. The useBlockendEvents hook returns a mitt instance that you can use to listen to widget events.

Setting Up Event Listeners

Basic Setup

import React, { useEffect } from "react";
import { useBlockendEvents, blockendEventsConstants } from "@blockend/widget";

function MyApp() {
  const eventEmitter = useBlockendEvents();

  useEffect(() => {
    // Subscribe to events
    const handleTransactionStarted = (data) => {
      console.log("Transaction started:", data);
    };

    eventEmitter.on(blockendEventsConstants.TransactionStarted, handleTransactionStarted);

    // Cleanup on unmount
    return () => {
      eventEmitter.off(blockendEventsConstants.TransactionStarted, handleTransactionStarted);
    };
  }, [eventEmitter]);

  return (
    <div>
      <Blockend />
    </div>
  );
}

Complete Event Listener Setup

Available Events

1. TransactionStarted

Event Name: blockendEventsConstants.TransactionStarted

When Triggered: When a user initiates a transaction through the widget

Example Usage:

2. TransactionSteps

Event Name: blockendEventsConstants.TransactionSteps

When Triggered: When transaction steps are created or when step-related errors occur

Data Structure:

Example Usage:

3. TransactionData

Event Name: blockendEventsConstants.TransactionData

When Triggered: When transaction data is fetched for each step or when data-related errors occur

Data Structure:

Example Usage:

4. TransactionStatus

Event Name: blockendEventsConstants.TransactionStatus

When Triggered: When polling for transaction status updates

Data Structure:

Example Usage:

5. SubmitSignedTxn

Event Name: blockendEventsConstants.SubmitSignedTxn

When Triggered: When a signed transaction is submitted to the blockchain

Data Structure:

Example Usage:

Advanced Usage Patterns

Transaction Progress Tracking

Best Practices

  1. Always Clean Up Event Listeners: Make sure to remove event listeners in the cleanup function to prevent memory leaks.

  2. Handle Error Cases: Always check for error properties in event data and handle them appropriately.

  3. Use Event Constants: Always use blockendEventsConstants instead of hardcoded strings to avoid typos.

Troubleshooting

Events Not Firing

  • Ensure you're importing the hook correctly: import { useBlockendEvents } from '@blockend/widget'

  • Verify that the widget is properly mounted before setting up event listeners

  • Check that you're using the correct event constants

Memory Leaks

  • Always clean up event listeners in useEffect cleanup functions

  • Avoid creating new handler functions on every render - use useCallback if necessary

TypeScript Errors

  • Make sure you have the latest version of the widget package

  • Import types properly: import type { ... } from '@blockend/widget'

Last updated

Was this helpful?