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)
A workaround I used was to use a boolean state variable, with initial value as
false. Its value changes fromfalsetotruewhen theconnectfunction is called. (connectfromuseConnecthook) And fromtruetofalsewhen thedisconnectfunction is called. (disconnectfromuseDisconnecthook)The
useConnecthook also returns a boolean valueisDisconnected. So, a network change makes the value ofisDisconnectedfalse, however the state variable still has a value true.