Skip to content

Commit 1586f15

Browse files
[Docs] Add guide for using ConnectButton with wagmi application
1 parent 6a7b556 commit 1586f15

File tree

1 file changed

+42
-3
lines changed
  • apps/portal/src/app/wallets/adapters

1 file changed

+42
-3
lines changed

apps/portal/src/app/wallets/adapters/page.mdx

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,48 @@ export const config = createConfig({
6363
});
6464
```
6565

66-
Then in your application, you can use the connector to trigger the in-app wallet connection.
66+
### Using the ConnectButton or ConnectEmbed with a wagmi application
67+
68+
You can use the thirdweb react connection components and hooks like ConnectButton, ConnectEmbed, useConnectModal, etc, but still keep using wagmi for the rest of the application.
69+
70+
To do so, you will need to connect the `inAppWalletConnector` connector from the `@thirdweb-dev/wagmi-adapter` package with the connected thirdweb wallet.
71+
72+
You can do this at any time after connecting the thirdweb wallet, a convenient place to do this is in the `onConnect` callback of the ConnectButton or ConnectEmbed.
73+
74+
```tsx
75+
const { connectors, connect } = useConnect(); // from wagmi
76+
77+
<ConnectButton
78+
client={client}
79+
onConnect={(wallet) => {
80+
// connect the wagmi connector with the connected thirdweb wallet
81+
const twConnector = connectors.find(
82+
(c) => c.id === "in-app-wallet",
83+
);
84+
if (twConnector) {
85+
const options = {
86+
wallet, // pass the connected wallet
87+
} satisfies ConnectionOptions; // for type safety
88+
connect({
89+
connector: twConnector,
90+
chainId: chain.id,
91+
...options,
92+
});
93+
}
94+
}}
95+
/>
96+
```
97+
98+
Make sure your app is wrapped in a `<ThirdwebProvider>` as well as a `<WagmiProvider>` since both contexts will be used here. From there, the wallet state will be in sync between the 2 libraries.
99+
100+
**Note:** the ConnectButton and ConnectEmbed handle reconnecting automatically on page reload. If not using those components (ie. useConnectModal or useConnect hooks directly), you will need to handle reconnecting by explicitely calling `useAutoConnect()`, and doing the same wagmi connection from the `onConnect` callback.
101+
102+
### Using the connector directly (headless mode)
103+
104+
You can also use the `inAppWalletConnector` connector directly in your application to connect to the thirdweb wallet without needing a `<ThirdwebProvider>` at all.
67105

68106
```ts
69-
const { connect, connectors } = useConnect();
107+
const { connect, connectors } = useConnect(); // from wagmi
70108

71109
const onClick = () => {
72110
// grab the connector
@@ -79,9 +117,10 @@ const onClick = () => {
79117
};
80118
```
81119

120+
82121
### Converting a wagmi wallet client to a thirdweb wallet
83122

84-
You can use the thirdweb SDK within a wagmi application by setting the wagmi connected account as the thirdweb 'active wallet'. After that, you can use all of the react components and hooks normally, including `BuyWidget`, `TransactionButton`, etc.
123+
You can also use the thirdweb SDK within a wagmi application by setting the wagmi connected account as the thirdweb 'active wallet'. After that, you can use all of the react components and hooks normally, including `BuyWidget`, `TransactionButton`, etc.
85124

86125
```ts
87126
// Assumes you've wrapped your application in a `<ThirdwebProvider>`

0 commit comments

Comments
 (0)