wagmi: Switch network doesn't work with non default chains

Is there an existing issue for this?

  • I have searched the existing issues

Package Version

0.2.7

Current Behavior

I added some new chains when setting up the providers.

When i use SwitchNetwork from the useNetwork hook with chains i added e.g BSC, BSC Testnet etc, the selected network and list of networks do not show any longer. There is no error and if i refresh the page, it now shows that i am connected to the right network

Expected Behavior

SwitchNetwork should work well with default and non default chains

Steps To Reproduce

Add new chains to the provider on setup and then use the code below to select and switch between chains

import { useNetwork } from 'wagmi'

const App = () => {
  const [{ data, error, loading }, switchNetwork] = useNetwork()

  return (
    <>
      <div>
        {data.chain?.name ?? networkData.chain?.id}{' '}
        {data.chain?.unsupported && '(unsupported)'}
      </div>

      {switchNetwork &&
        data.chains.map((x) =>
          x.id === data.chain?.id ? null : (
            <button key={x.id} onClick={() => switchNetwork(x.id)}>
              Switch to {x.name}
            </button>
          ),
        )}

      {error && <div>{error?.message}</div>}
    </>
  )
}

Link to Minimal Reproducible Example (CodeSandbox, StackBlitz, etc.)

No response

Anything else?

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 9
  • Comments: 15 (8 by maintainers)

Most upvoted comments

I’m facing the same issue in my project, any workaround meanwhile MM fix it? Thanks in advance!

A workaround I used was to use a boolean state variable, with initial value as false. Its value changes from false to true when the connect function is called. (connect from useConnect hook) And from true to false when the disconnect function is called. (disconnect from useDisconnect hook)

The useConnect hook also returns a boolean value isDisconnected. So, a network change makes the value of isDisconnected false, however the state variable still has a value true.