Skip to content

Commit 9524dad

Browse files
authored
Update docs to include info for disconnect method (#847)
* Add disconnect function to react package * Add disconnect function to react package * Update docs for disconnect method * Update docs with disconnect impl example * update disconnect method params * update docs more for disconnect
1 parent 362170a commit 9524dad

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

packages/react/README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const web3Onboard = init({
9999
})
100100

101101
function App() {
102-
const [{ wallet, connecting }, connect] = useConnectWallet()
102+
const [{ wallet, connecting }, connect, disconnect] = useConnectWallet()
103103
const [{ chains, connectedChain, settingChain }, setChain] = useSetChain()
104104
const connectedWallets = useWallets()
105105

@@ -125,6 +125,9 @@ function App() {
125125
})}
126126
</select>
127127
)}
128+
<button onClick={() => disconnect(wallet)}>
129+
Disconnect Wallet
130+
</button>
128131
</div>
129132
)}
130133

@@ -156,13 +159,18 @@ import { useConnectWallet } from '@web3-onboard/react'
156159

157160
type UseConnectWallet = (): [
158161
{ wallet: WalletState | null; connecting: boolean },
159-
(options: ConnectOptions) => Promise<void>
162+
(options: ConnectOptions) => Promise<void>,
163+
(wallet: DisconnectOptions) => Promise<void>
160164
]
161165

162166
type ConnectOptions = {
163167
autoSelect?: string // wallet name to autoselect for user
164168
}
165169

170+
type DisconnectOptions = {
171+
label: string // wallet label
172+
}
173+
166174
type WalletState = {
167175
label: string
168176
icon: string
@@ -177,7 +185,8 @@ const [
177185
wallet, // the wallet that has been connected or null if not yet connected
178186
connecting // boolean indicating if connection is in progress
179187
},
180-
connect // function to call to initiate user to connect wallet
188+
connect, // function to call to initiate user to connect wallet
189+
disconnect // function to call to with wallet<DisconnectOptions> to disconnect wallet
181190
] = useConnectWallet()
182191
```
183192

packages/react/src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
} from '@web3-onboard/core'
1111

1212
import { Chain } from '@web3-onboard/common'
13+
import { DisconnectOptions } from '@web3-onboard/core/dist/types'
1314

1415
export let web3Onboard: OnboardAPI | null = null
1516

@@ -21,7 +22,7 @@ export const init = (options: InitOptions): OnboardAPI => {
2122
export const useConnectWallet = (): [
2223
{ wallet: WalletState | null; connecting: boolean },
2324
(options: ConnectOptions) => Promise<void>,
24-
(wallet: WalletState) => Promise<void>
25+
(wallet: DisconnectOptions) => Promise<void>
2526
] => {
2627
if (!web3Onboard) throw new Error('Must initialize before using hooks.')
2728

@@ -42,17 +43,14 @@ export const useConnectWallet = (): [
4243
}
4344
}, [])
4445

45-
const disconnect = useCallback(async wallet => {
46+
const disconnect = useCallback(async ({label}) => {
4647
setConnecting(true)
4748

48-
await (
49-
web3Onboard as OnboardAPI
50-
).disconnectWallet({ label: wallet.label })
49+
await (web3Onboard as OnboardAPI).disconnectWallet({label})
5150

5251
setConnectedWallet(null)
5352

5453
setConnecting(false)
55-
5654
}, [])
5755

5856
return [{ wallet, connecting }, connect, disconnect]

0 commit comments

Comments
 (0)