Skip to content

Commit 5c6e4df

Browse files
committed
feat(react): return isReconnecting from useWallet
1 parent 9869279 commit 5c6e4df

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

docs/guides/react-quick-start.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ Now, in any component, you can use the `useWallet` hook to access the wallet man
4949
import { useWallet } from '@txnlab/use-wallet-react'
5050

5151
function WalletMenu() {
52-
const { wallets, activeWallet, activeAccount } = useWallet()
52+
const { wallets, activeWallet, activeAccount, isReconnecting } = useWallet()
53+
54+
if (isReconnecting) {
55+
return <div>Reconnecting to wallet...</div>
56+
}
5357

5458
return (
5559
<div>
@@ -76,6 +80,8 @@ function WalletMenu() {
7680
}
7781
```
7882
83+
The `isReconnecting` state is a boolean that helps manage wallet reconnection flows. It starts as `true` during both SSR and initial client-side mounting. During the first mount, the `WalletProvider` automatically attempts to restore any previously connected wallet sessions. Once this reconnection process completes - whether successful or not - `isReconnecting` switches to `false`.
84+
7985
## Signing Transactions
8086
8187
To sign and send transactions, you can use the library's `algodClient` instance and the `transactionSigner` provided by the active wallet.

packages/use-wallet-react/src/index.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from '@txnlab/use-wallet'
77

88
interface IWalletContext {
99
manager: WalletManager
10+
isReconnecting: boolean
1011
algodClient: algosdk.Algodv2
1112
setAlgodClient: React.Dispatch<React.SetStateAction<algosdk.Algodv2>>
1213
}
@@ -33,7 +34,7 @@ export const useWallet = () => {
3334
throw new Error('useWallet must be used within the WalletProvider')
3435
}
3536

36-
const { manager, algodClient, setAlgodClient } = context
37+
const { manager, isReconnecting, algodClient, setAlgodClient } = context
3738

3839
const activeNetwork = useStore(manager.store, (state) => state.activeNetwork)
3940

@@ -108,6 +109,7 @@ export const useWallet = () => {
108109

109110
return {
110111
wallets,
112+
isReconnecting,
111113
algodClient,
112114
activeNetwork,
113115
activeWallet,
@@ -129,6 +131,7 @@ interface WalletProviderProps {
129131

130132
export const WalletProvider = ({ manager, children }: WalletProviderProps): JSX.Element => {
131133
const [algodClient, setAlgodClient] = React.useState(manager.algodClient)
134+
const [isReconnecting, setIsReconnecting] = React.useState(true)
132135

133136
React.useEffect(() => {
134137
manager.algodClient = algodClient
@@ -142,6 +145,8 @@ export const WalletProvider = ({ manager, children }: WalletProviderProps): JSX.
142145
await manager.resumeSessions()
143146
} catch (error) {
144147
console.error('Error resuming sessions:', error)
148+
} finally {
149+
setIsReconnecting(false)
145150
}
146151
}
147152

@@ -152,7 +157,7 @@ export const WalletProvider = ({ manager, children }: WalletProviderProps): JSX.
152157
}, [manager])
153158

154159
return (
155-
<WalletContext.Provider value={{ manager, algodClient, setAlgodClient }}>
160+
<WalletContext.Provider value={{ manager, isReconnecting, algodClient, setAlgodClient }}>
156161
{children}
157162
</WalletContext.Provider>
158163
)

0 commit comments

Comments
 (0)