useSwitchChain() hook does not work

  • SDK Version(package.json): latest
  • Platform: React SDK

Currently, Web3Auth’s useSwitchChain() hook imported from @web3auth/modal/react does not properly work. switchChain() successfully changes the Wagmi state. That is, const { chain } = useAccount() shows the correct chain after switching. However, web3Auth.provider.chain from const {web3Auth } = useWeb3Auth() remains on the old chain. This is in a Next.js App.

This leads to several problems. For one, you cannot set up multiple chains for Smart Accounts because web3Auth.accountAbstractionProvider.smartAccount and web3Auth.accountAbstractionProvider.bundlerClient will not use the updated chain.

A workaround is to create your own Smart Account and Bundler Client. That is, do not use Web3Auth’s native Smart Account. This method also allows you to use any Smart Account provider, so it doesn’t have to be MetaMask’s. I detailed the workaround here:

Thank you for your detailed report, @brianonchain! Here’s a summary of your issue for the support team:


Issue Summary

  • SDK: Web3Auth Plug and Play (PnP) Web SDK
  • SDK Version: latest (from package.json)
  • Platform: React (using @web3auth/modal/react)
  • Issue:
    • The useSwitchChain() hook updates the Wagmi state (i.e., useAccount().chain reflects the correct chain after switching).
    • However, web3Auth.provider.chain (from useWeb3Auth()) does not update and remains on the old chain.
    • This causes issues with Smart Accounts, as web3Auth.accountAbstractionProvider.smartAccount and web3Auth.accountAbstractionProvider.bundlerClient do not use the updated chain.
  • Workaround:
    • Manually creating your own Smart Account and Bundler Client works, as described in your Medium article.

Additional Context

  • You referenced the Web3Auth documentation and confirmed standard initialization.
  • No custom authentication or JWT usage mentioned.
  • No code snippets or screenshots provided, but the behavior is clearly described.

Next Steps / Suggestions

  • For the Support Team:

    • Please confirm if this is a known limitation or bug with the useSwitchChain() hook and the provider state.
    • Is there a recommended way to force web3Auth.provider to re-sync with the new chain after switching?
    • If not, consider documenting this limitation or providing an official workaround.
  • For the User:

    • If possible, please share a minimal code snippet showing your usage of useSwitchChain() and how you access web3Auth.provider.chain. This may help with debugging or providing a workaround.

Thank you for your detailed feedback and for sharing your workaround with the community! If you have any additional details (such as code snippets or specific error messages), please feel free to add them here to assist the support team further.

Related Docs:


The support team will review this and follow up with you soon!

Note: This response was generated by an AI bot and may require further verification. The Web3Auth support team has tracked this issue and will be responding as soon as possible.

Additionally, we would recommend you to join our biweekly Office Hours to get this resolved even faster. Web3Auth Developer Office Hours