preloader

Group Buzz

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

Updating MetaMask Notification in a React App

As a developer building decentralized applications (dapps), you are probably familiar with the importance of ensuring seamless interactions between your users and your application. One critical aspect is receiving notifications from external services, such as MetaMask, when certain events occur within your app. In this article, we will explore how to implement MetaMask notifications in a React app.

Why do we need MetaMask notifications?

MetaMask is the popular browser extension for Ethereum users. When you initiate transactions on the Ethereum network using MetaMask, you can expect various responses from external services, such as:

  • Transaction Confirmation – The transaction was successful.
  • Transaction Decline

    – The transaction was declined due to a validation error or other issues.

  • Error Messages – A specific error message related to the transaction.

Setting up MetaMask notifications

To receive these notifications in your React app, you will need to set up MetaMask notifications. Here’s how:

  • Install the @metamask/notifications package:

npm install @metamask/notifications

  • Import the Notifications class and initialize it with the necessary configuration:

import { Notifications } from '@metamask/notifications';

const notifications = new Notifications({

// Set the notification display settings here

displaySettings: {

timeToHide: 5, // seconds

timeout: 5000, // milliseconds

},

});

Handling MetaMask notifications in a React app

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

Now that we have set up the Notifications class, let’s show how to handle these notifications in a React app. We’ll create a simple example with the TransactionSuccess and TransactionError event handlers.

“`javascript

import React, { useState } from ‘react’;

import MetaMask from ‘@metamask/notifications’;

const App = () => {

const [transactions, setTransactions] = useState([]);

const [transactionType, setTransactionType] = useState(”);

const handleTransactionSuccess = (transaction) => {

// Update the UI with a success message

console.log(‘Transaction succeeded:’, transaction);

setTransactions((prevTransactions) => […prevTransactions, transaction]);

notifications.clear();

};

const handleTransactionError = (error) => {

// Display an error message in your app

console.error(‘Transaction error:’, error);

notifications.show({

type: ‘error’,

title: ‘Error’,

content: ‘Failed to process transaction.’,

});

};

return (

MetaMask Notifications Example

{/ Initialize MetaMask notifications /}

showNotifications={true}

onTransactionSuccess={handleTransactionSuccess}

onTransactionError={handleTransactionError}

/>

{/ Display transactions received by MetaMask /}

    {transactions.map((transaction) => (

  • Transaction type: {transaction.type} | Time: {transaction.time}

  • ))}

{/ Update UI with success message after some time /}

{/ Display error messages from MetaMask /}

Leave a Reply

Your email address will not be published. Required fields are marked *

User Login

Lost your password?
Cart 0