Skip to content

Commit aff0a46

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

File tree

1 file changed

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

1 file changed

+40
-3
lines changed

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,46 @@ 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+
### Using the connector directly (headless mode)
101+
102+
You can also use the `inAppWalletConnector` connector directly in your application to connect to the thirdweb wallet without needing a `<ThirdwebProvider>` at all.
67103

68104
```ts
69-
const { connect, connectors } = useConnect();
105+
const { connect, connectors } = useConnect(); // from wagmi
70106

71107
const onClick = () => {
72108
// grab the connector
@@ -79,9 +115,10 @@ const onClick = () => {
79115
};
80116
```
81117

118+
82119
### Converting a wagmi wallet client to a thirdweb wallet
83120

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.
121+
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.
85122

86123
```ts
87124
// Assumes you've wrapped your application in a `<ThirdwebProvider>`

0 commit comments

Comments
 (0)