This guide will show you how you can use the Fuel Wallet SDK and its React Hooks to build a simple React application that lets users connect their wallet to your application and see their balance.
The first thing we will do is setup a Next.js project.
pnpm create next-app my-fuel-app
Next, we will install the Fuel Wallet React SDK and the Fuel TypeScript SDK.
pnpm add @fuel-wallet/react fuels@0.73.0
In order to make use of the React hooks provided by the Fuel Wallet SDK, we need to wrap our application in a FuelProvider
component. This component will provide the hooks with the necessary context to interact with the Fuel Wallet SDK. Add the following to your pages/_app.tsx
file:
export default function App({ Component, pageProps }: AppProps) {
return (
<FuelProvider>
<Component {...pageProps} />
</FuelProvider>
);
}
Go to your pages/index.tsx
file and replace the contents with the following:
import {
useAccount,
useBalance,
useConnect,
useConnectors,
useDisconnect,
useIsConnected,
} from "@fuel-wallet/react";
import { useState } from "react";
export default function Home() {
const [connector, setConnector] = useState("");
const { connectors } = useConnectors();
const { connect } = useConnect();
const { disconnect } = useDisconnect();
const { isConnected } = useIsConnected();
const { account } = useAccount();
const { balance } = useBalance({
address: account as string,
});
return (
<div
style={{
display: "flex",
flexDirection: "column",
gap: 10,
padding: 10,
maxWidth: 300,
}}
>
<select
onChange={(e) => {
setConnector(e.target.value);
}}
>
<option value="">Select a connector</option>
{connectors.map((c) => (
<option key={c.name} value={c.name}>
{c.name}
</option>
))}
</select>
<button disabled={!connector} onClick={() => connect(connector)}>
Connect to {connector}
</button>
<button disabled={!connector} onClick={() => disconnect()}>
Disconnect from {connector}
</button>
<p>{isConnected ? "Connected" : ""}</p>
{account && <p>Account: {account}</p>}
{balance && <p>Balance: {balance.toString()}</p>}
</div>
);
}
Let's break down what's happening here.
The useConnectors
hook returns a list of available wallet connectors.
Once a connector has been selected by the user, the useConnect
hook will return a connect
function that can be used to connect the user's wallet to your application.
The useAccount
hook returns information about the user's account, if they are connected.
The useBalance
hook returns the user's ETH balance on the beta-5
network , if they are connected.