Integrate Freighter with a React dapp
Wallets are an essential part of any dapp. They allow users to interact with the blockchain and sign transactions. In this section, you'll learn how to integrate the Freighter wallet into your React dapps.
WalletData Component
In the example crowdfund dapp, the WalletData
component plays a key role in wallet integration. Let's break down the code and understand its functionality:
import React from "react";
import { useAccount, useIsMounted } from "../../../hooks";
import { ConnectButton } from "../../atoms";
import styles from "./style.module.css";
export function WalletData() {
const mounted = useIsMounted();
const account = useAccount();
return (
<>
{mounted && account ? (
<div className={styles.displayData}>
<div className={styles.card}>{account.displayName}</div>
</div>
) : (
<ConnectButton label="Connect Wallet" />
)}
</>
);
}
Here's a breakdown of the code:
- The
mounted
variable is obtained using theuseIsMounted
hook, indicating whether the component is currently mounted or not. - The
useAccount
hook is used to fetch the user's account data, and thedata
property is destructured from the result. - Conditional rendering is used to display different content based on the component's mount status and the availability of account data.
- If the component is mounted and the account data is available, the user's wallet data is displayed. This includes the account's display name.
- If the component is not mounted or the account data is not available, a
ConnectButton
component is rendered, allowing the user to connect with Freighter.
Guides in this category:
📄️ Connect to the Testnet
1. Install the Freighter browser extension.
📄️ Enable Soroban tokens
With a funded Stellar account, you can now add Soroban tokens to your Freighter wallet.
📄️ Integrate Freighter with a React dapp
Wallets are an essential part of any dapp. They allow users to interact with the blockchain and sign transactions. In this section, you'll learn how to integrate the Freighter wallet into your React dapps.
📄️ As a dapp developer, prompt Freighter to sign transactions
If you're building a JS dapp, easily sign Soroban transactions using the Freighter browser extension and its corresponding client library @stellar/freighter-api:
📄️ Send Soroban token payments
Once you have added a Soroban token to your Freighter wallet, you can now send a payment of that token directly from Freighter.
📄️ Sign authorization entries
In order to take advantage of contract authorization, you can use Freighter's API to sign an authorization entry. A good example of how signing an authorization entry works can be found in the authorizeEntry helper of stellar-sdk.
📄️ Sign Soroban XDRs
With a funded Testnet account, you can now sign Soroban XDRs using dApps that are integrated with Freighter. An example of an integrated dApp is Stellar's Laboratory.