fix(wallet-adapter-react): support async autoConnect and late wallet injection #726
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes multiple issues with
autoConnectfunctionality that prevented automatic wallet reconnection in certain scenarios.Problems Fixed
1. Async
autoConnectprop not workingIf
autoConnectstarted asfalseand later becametrue(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
AutoConnectProviderin the example apps had a race condition wherelocalStorage.removeItemwas called on initial mount beforelocalStorage.getItemcould read the stored value.Solution
Moved
didAttemptAutoConnectRef.current = trueto only be set when:This allows the autoConnect effect to retry when:
autoConnectprop changes fromfalsetotruestandardWalletsAddedeventChanges
packages/wallet-adapter-react/src/WalletProvider.tsxpackages/wallet-adapter-react/tests/WalletProvider.test.tsxapps/nextjs-example/src/components/AutoConnectProvider.tsxapps/nextjs-x-chain/src/components/AutoConnectProvider.tsxTesting
Note
Medium Risk
Changes connection initialization and
isLoadingbehavior inWalletProvider, 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 whenautoConnectis enabled asynchronously and when a wallet extension injects late.Stabilizes loading state and examples. Adds an
initialLoadCompletedguard to avoid the auto-connect effect resettingisLoadingduring user-initiatedconnect()calls, and fixes example appAutoConnectProviderlocalStorage initialization so the stored preference isn’t deleted on first mount. ExpandedWalletProvidertests 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.