Usage with React Native SDK
Quick Started
The quickest way to get started is to use the interactive Connect Wallet component builder. It shows React specifically but most of the components are present in React Native.
This playground allows you to modify the theme, size, and supported wallets of the Connect Wallet component and preview the changes in real time.
You can then copy the code snippet and add it directly to your app.
For details on how to use the Connect Wallet component in an app and set up a project, read the guide below and use your generated code snippet for the ConnectWallet
component.
Guide
When you want to use a wallet with React Native SDK - Instead of importing the wallet from @thirdweb-dev/wallets
, you need to import its “wallet configurator” function from the @thirdweb-dev/react-native
package.
This wallet configurator function contains additional metadata and UI for connecting the wallet which is shown in the ConnectWallet component's Modal.
You can find the list of all wallet configurators for React Native SDK here
You can connect to a wallet in two ways:
- using ConnectWallet component for a prebuilt solution with UI
- using useConnect hook to build a custom solution
1. Using ConnectWallet
ConnectWallet component allows you to connect to wallets that are specified in ThirdwebProvider
Clicking on the ConnectWallet
button shows wallets in a Modal and shows wallet-specific UI for connecting the wallet when it is selected (This UI is defined in the wallet configurator itself, not in ConnectWallet
component)
Example
import {
ThirdwebProvider,
metamaskWallet,
trustWallet,
coinbaseWallet,
} from "@thirdweb-dev/react-native";
// provide supported wallets to ThirdwebProvider
function MyApp() {
return (
<ThirdwebProvider
supportedWallets={[
metamaskWallet(),
trustWallet(),
coinbaseWallet({
callbackURL: new URL("your-callback-url"),
}),
]}
clientId="your-client-id"
>
<App />
</ThirdwebProvider>
);
}
import { ConnectWallet } from "@thirdweb-dev/react-native";
function App() {
return <ConnectWallet />;
}
2. Using useConnect hook
useConnect hook allows you to programmatically connect to the wallet. You will need to build your own UI for connecting the wallet.
import { useConnect, metamaskWallet } from "@thirdweb-dev/react-native";
const metamask = metamaskWallet();
function App() {
const connect = useConnect();
return (
<Button
title="Connect to MetaMask"
onPress={async () => {
const wallet = await connect(metamask, connectOptions);
console.log("connected to ", wallet);
}}
/>
);
}
3. Using ThirdwebSDKProvider (advanced)
ThirdwebSDKProvider is a lower level provider that accepts an arbitrary wallet and only handles contract interaction. This means you can use the Wallet SDK to programmatically connect to any wallet, and pass the connected wallet to this provider to execute transactions from the connected wallet.
import { ThirdwebSDKProvider } from "@thirdweb-dev/react-native";
import { Goerli } from "@thirdweb-dev/chains";
import { Signer } from "ethers";
function MyComponent() {
const [signer, setSigner] = useState<Signer | null>(null);
// in this example we just generate a random wallet
useEffect(() => {
const generateWallet = async () => {
const localWallet = new LocalWallet({
chain: Goerli,
});
localWallet.generate();
localWallet.connect();
setSigner(await localWallet.getSigner());
};
generateWallet();
}, []);
// The provider will handle maintaining the connected wallet
// calling contracts from within the provider with the regular hooks
// will execute on behalf of the provided wallet signer
return (
<ThirdwebSDKProvider
activeChain={Goerli}
signer={signer}
clientId="your-client-id"
>
<App />
</ThirdwebSDKProvider>
);
}
If you are building your own wallet and want to use it with React Native SDK you will need to create a wallet configurator as mentioned in Build a wallet guide where you can define a custom UI for connecting your wallet that will be shown in the ConnectWallet modal