Creating a simple React application that integrates with MultiversX, allows user sign-in/sign-out, and facilitates sending EGLD transactions involves several steps. I'll outline the process and provide code snippets for each part of the application.
This React app will utilize useElven for connecting to MultiversX and handling authentication and transactions. You'll need to have Node.js and npm installed on your system to follow along.
Step 1: Create a New React App
First, create a new React application using Create React App:
npx create-react-app multiversx-app
cd multiversx-app
Step 2: Install useElven Dependency
Install @useelven/core to integrate with MultiversX:
npm install @useelven/core
Step 3: Set Up useElven
In your App.js, import and initialize useElven:
import React from 'react';
import { useLogin, useTransaction } from '@useelven/core';
function App() {
const { login, isLoggedIn, logout } = useLogin();
const { triggerTx, pending } = useTransaction();
// UI states (e.g., showing pending state, handling form submission)
// ...
return (
<div className="App">
{
isLoggedIn ?
<LoggedInView
onSendEgld={handleSendEgld}
onLogout={logout}
isLoading={pending}
/> :
<LoginView onLogin={login} />
}
</div>
);
}
// Components for LoggedInView and LoginView
// ...
export default App;
Step 4: Implement Login and Logout UI
Create components for login and logout views:
function LoginView({ onLogin }) {
return (
<div>
<button onClick={() => onLogin()}>Login</button>
</div>
);
}
function LoggedInView({ onSendEgld, onLogout, isLoading }) {
// Form submission logic and UI
// ...
return (
<div>
<button onClick={onLogout}>Logout</button>
{/* Form for EGLD transfer */}
{/* Loading indicator if isLoading is true */}
</div>
);
}
Step 5: Implement EGLD Transfer Functionality
In the LoggedInView, add a form to accept the address and amount, and handle submission:
// Inside LoggedInView component
const handleFormSubmit = (event) => {
event.preventDefault();
const address = event.target.address.value;
const amount = event.target.amount.value;
onSendEgld(address, amount);
};
return (
<form onSubmit={handleFormSubmit}>
<input name="address" type="text" placeholder="Address" required />
<input name="amount" type="number" placeholder="Amount" required />
<button type="submit">Send EGLD</button>
</form>
);
Step 6: Sending EGLD Transaction
Implement the handleSendEgld function in App.js to create and send the EGLD transaction:
const handleSendEgld = async (address, amount) => {
// Use triggerTx from useTransaction to send EGLD
// ...
try {
await triggerTx({
address,
value: amount,
// Additional parameters like gasLimit, data, etc.
});
} catch (error) {
console.error('Transaction failed:', error);
}
};
Step 7: Managing UI States
Handle different UI states (e.g., pending, error) using React state hooks. Show appropriate messages or loaders based on these states.
Step 8: Run the App
Finally, start your React application:
npm start
This app will now let users log in, send EGLD to a specified address, and log out. Remember to handle errors and edge cases appropriately for a production-level application. This example provides a basic structure and may require further customization based on your specific requirements.