Skip to content

Conversation

@0xmaayan
Copy link
Contributor

@0xmaayan 0xmaayan commented Jan 31, 2026

Summary

Fixes multiple issues with autoConnect functionality that prevented automatic wallet reconnection in certain scenarios.

Problems Fixed

1. Async autoConnect prop not working

If autoConnect started as false and later became true (e.g., after fetching user preferences from an API or localStorage hydration in SSR apps), auto-connect would never trigger.

2. Late wallet injection not working

If a wallet extension injected after the initial render (due to network latency, slow script loading, etc.), auto-connect would fail because the attempt was already marked as "done" before the wallet registered.

3. localStorage race condition in example apps

The AutoConnectProvider in the example apps had a race condition where localStorage.removeItem was called on initial mount before localStorage.getItem could read the stored value.

Solution

Moved didAttemptAutoConnectRef.current = true to only be set when:

  1. There's no stored wallet name (nothing to connect to)
  2. We actually find the wallet and are about to connect

This allows the autoConnect effect to retry when:

  • autoConnect prop changes from false to true
  • New wallets register via the standardWalletsAdded event

Changes

File Change
packages/wallet-adapter-react/src/WalletProvider.tsx Fix autoConnect logic to support async and late wallet scenarios
packages/wallet-adapter-react/tests/WalletProvider.test.tsx Added 3 new tests for async autoConnect scenarios
apps/nextjs-example/src/components/AutoConnectProvider.tsx Fix localStorage race condition
apps/nextjs-x-chain/src/components/AutoConnectProvider.tsx Fix localStorage race condition

Testing

  • ✅ 61 unit tests pass
  • ✅ Manual testing with Petra wallet confirms auto-reconnect works

Note

Medium Risk
Changes connection initialization and isLoading behavior in WalletProvider, which can affect wallet reconnection timing and UX across dApps. Added tests reduce regression risk but behavior changes are user-facing.

Overview
Improves auto-connect reliability in wallet-adapter-react. The provider now delays marking auto-connect as “attempted” until there’s either no stored wallet to reconnect to or an installed wallet is actually found, enabling retries when autoConnect is enabled asynchronously and when a wallet extension injects late.

Stabilizes loading state and examples. Adds an initialLoadCompleted guard to avoid the auto-connect effect resetting isLoading during user-initiated connect() calls, and fixes example app AutoConnectProvider localStorage initialization so the stored preference isn’t deleted on first mount. Expanded WalletProvider tests cover async prop changes, late wallet registration, and the loading-state regression.

Written by Cursor Bugbot for commit 49e749f. This will update automatically on new commits. Configure here.

@0xmaayan 0xmaayan force-pushed the fix/async-autoconnect branch 2 times, most recently from 6e37b3b to e93fe73 Compare January 31, 2026 12:59
@0xmaayan 0xmaayan force-pushed the fix/async-autoconnect branch from e93fe73 to a0bd1ff Compare January 31, 2026 13:18
@0xmaayan 0xmaayan changed the title fix(wallet-adapter-react): support async autoConnect prop values fix(wallet-adapter-react): support async autoConnect and late wallet injection Jan 31, 2026
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@0xmaayan 0xmaayan force-pushed the fix/async-autoconnect branch from a0bd1ff to 49e749f Compare January 31, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants