Metamas notification update in the React application
As a developer building decentralized applications (DAPPS), you probably know the importance of ensuring smooth interaction between users and the application. One of the key aspects is to receive notifications from external services, such as Metamask, when some events appear in your application. In this article, we will examine how to implement Metamask notifications in the React application.
Why do we need Metamas notifications?
Metamask is a popular browser extension for Ethereum users. When initiating transactions in the Ethereum network using a metamask, you can expect various answers from external services, such as:
- Confirmation of the transaction : The transaction was successful.
- Rejection of transactions : The transaction was rejected due to the error of checking correctness or other problems.
- error messages : a specific error message related to the transaction.
Configuring Metamask notifications
To receive these notifications in the React application, you need to configure Metamask notifications. Here’s how:
- Install the “@metamask/notifications” package:
`Bash
NPM Install @metamask/notifications
`
- Apply the class of “notifications” and initiate it with the necessary configuration:
`Javascript
Import {notifications} with "@Metamask/Notifications";
Const Corforms = new notifications ({{
// Set display settings notification here
Displays: {
Timetohide: 5, // seconds
Time limit: 5000, // Miliseconds
},
});
Metamask notification support in the React application
Now that we have a “notification” class configuration, let’s desek how to deal with these notifications in the React application. We will create a simple example with “TransationSuCcess” and “Transactionerror” events.
“ Javascript
Import React, {Usestate} with “React”;
import metamask with “@metamask/notifications”;
const app = () => {
Const [transactions, Setransactions] = Usestate ([]);
Const [Transactiontype, Setransationaltype] = Usestate (”);
const handletransationSuCCESS = (transaction) => {
// update the user interface about the success message
console.log (“effective transaction:”, transaction);
Settransactions ((prevtransiations) => [… prevtransctions, transaction]);
notifications. Clear ();
};
const handletransactionerror = (error) => {
// View message about error in your application
console.error (“transaction error:”, error);
Notifications.Show ({{
Type: “error”,
Title: “Error”,
Content: “The transaction could not be processed”,
});
};
return (
Example of metamas notifications h1>
{/ Initiate notifications Metamask /}
Shown
ontransationSuCCESS = {dandletransationSuCESSS}
ontransactionerror = {dandletransactionerror}
/>
{/ Transactions displayed from Metamask /}
-
Transaction type: {transaction.tipe} | Time: {transaction.time}
{transactions.map (transaction) => (
)}
{/ Update the user interface about the success message after some time /}
Setransationaltype (“Success”);
}, 2000)}> Wait for 2 seconds before displaying the type of transaction
{/ View Metamask errors messages /}
-
Transaction type: {transaction.tipe} | Error code: {transaction.
{transactions.map (transaction) => (