Skip to content

Commit 3075629

Browse files
authored
2.0.5 - Fix missing initial states in react hooks (#886)
* fix missing initial states in react hooks * fix wallet in useConnectWallet not updating * remove optional chaining * fix version bump
1 parent 1eac5c7 commit 3075629

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

packages/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/react",
3-
"version": "2.0.4",
3+
"version": "2.0.5",
44
"description": "Collection of React Hooks for web3-onboard",
55
"module": "dist/index.js",
66
"browser": "dist/index.js",

packages/react/src/index.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,15 @@ export const useConnectWallet = (): [
2626
] => {
2727
if (!web3Onboard) throw new Error('Must initialize before using hooks.')
2828

29-
const [wallet, setConnectedWallet] = useState<WalletState | null>(null)
29+
const [wallet, setConnectedWallet] = useState<WalletState | null>(
30+
() => (web3Onboard as OnboardAPI).state.get().wallets[0] || null
31+
)
3032
const [connecting, setConnecting] = useState(false)
3133

3234
useEffect(() => {
3335
const subscription = (web3Onboard as OnboardAPI).state
3436
.select('wallets')
35-
.subscribe(wallets => {
36-
if (!wallet) return
37-
38-
const updatedWallet = wallets.find(
39-
({ label }) => label === wallet.label
40-
)
41-
42-
updatedWallet && setConnectedWallet(updatedWallet)
43-
})
37+
.subscribe(wallets => setConnectedWallet(wallets[0] || null))
4438

4539
return () => subscription.unsubscribe()
4640
}, [wallet])
@@ -89,7 +83,16 @@ export const useSetChain = (
8983
const [settingChain, setInProgress] = useState<boolean>(false)
9084

9185
const [connectedChain, setConnectedChain] = useState<ConnectedChain | null>(
92-
null
86+
() => {
87+
const initialWallets = (web3Onboard as OnboardAPI).state.get().wallets
88+
if (initialWallets.length === 0) return null
89+
return (
90+
(
91+
initialWallets.find(({ label }) => label === walletLabel) ||
92+
initialWallets[0]
93+
).chains[0] || null
94+
)
95+
}
9396
)
9497

9598
const chains = useMemo(() => state.get().chains, [])
@@ -119,7 +122,9 @@ export const useSetChain = (
119122
export const useWallets = (): WalletState[] => {
120123
if (!web3Onboard) throw new Error('Must initialize before using hooks.')
121124

122-
const [wallets, setConnectedWallets] = useState<WalletState[]>([])
125+
const [wallets, setConnectedWallets] = useState<WalletState[]>(
126+
() => (web3Onboard as OnboardAPI).state.get().wallets
127+
)
123128

124129
useEffect(() => {
125130
const wallets$ = (web3Onboard as OnboardAPI).state.select('wallets')

0 commit comments

Comments
 (0)