Metamask: How to get notification from MetaMask either transaction was successful or not in React app?

Home > Metamask: How to get notification from MetaMask either transaction was successful or not in React app?

Update Metamask Statement in ReCT app

As a developer that forms decentralized applications (DAPP), you are probably familiar with the importance of ensuring an invisible interaction between users and your application. One critical aspect is receiving statements from external services, such as Metamask, when some events are taking place in your app. In this article, we will study how to react to the Metamask notifications app.

Why do we need Metamass notifications?

Metamk is a popular browser extension for Ethereum users. When starting transactions on the Ethereum network using Metamask, you can expect a variety of answers from external services, such as:

1
The transaction confirmation : The transaction was successful.

  • Dealing of the transaction : The transaction was rejected due to validation error or other problems.

3
Error reports : A specific error message related to the transaction.

Metamask statements Setup

You will need to make Metamask notifications to receive these notifications in your React app. Here’s how:

  • Install the package @metamk/notifications :::

bash

NPM Install @Metamk/Notifications

`

  • Import the “notification” class and initialize it with the required configuration:

`JavaScript

Import {notifications} from “@metamk/notifications”;

Const notifications = new notifications ({

// Set the notification display settings here

Displaysettings: {

Timetohide: 5, // Seconds

Turn off: 5000, // Milisekundes

},

});

`

Metamk notification processing in ReCT app

Now that we have a class “Notifications”, let’s show you how to handle these statements in the React app. We will create a simple example with TransactionUcess and TransactionEror event processors.

`JavaScript

Import to react, {usestate} from ‘REACT’;

Import Metamku from@Metamk/Notifications;

Const app = () => {

Const [transactions, settransactions] = usestate ([]);

Const [Transactionype, Settransactype] = Usestate (”);

Const Handlapransactionsuccess = (Deal) => {

// Update the user interface with the success message

Console.log (‘Transaction Successful:’, Deal);

settransactions ((prevransactions) => [… Prevransactions, transaction]);

notifications.Clear ();

};

CONST HandletransactionEror = (Error) => {

// Show the error message in your app

Console.Error (‘A transaction error:’, error);

notifications.show ({

Type: “Error”,

Title: “Error”,

Content: “Failed to process a deal.”,

});

};

return (

Metamask statements

{/ Initializes Metamask notifications /}

The rule shown = {TRUE}

Ontransactionsuccess = {HandlationsUCCESS}

OnTransactionEror = {HandletransactionError}

/>

{/ Display transactions received from Metamk /}

    {transaction.map ((transaction) => (

  • Type of transaction: {transaction.Type} | Time: {transaction.time}

  • ))}

{/ Update the user interface with the success message after some time /}

More Posts

Share:

Send Us A Message