In a project built with React, Next.js, RainbowKit, and Wagmi, when BSC (Binance Smart Chain) is designated as the initial chain on H5 mobile terminals, the Ethereum Mainnet is consistently displayed upon connecting to the MetaMask wallet app, and the switch to the BSC chain cannot be completed successfully. #4916
Replies: 1 comment
-
|
This is a known issue with MetaMask Mobile's handling of Why it happensWhen connecting via WalletConnect (which RainbowKit uses), the chain switching happens in two steps:
MetaMask Mobile processes these sequentially, and on some versions it acknowledges the connection but drops or ignores the chain switch request — especially on mobile H5 (in-app browser). Fix: Force chain switch after connectionUse wagmi's import { useAccount, useSwitchChain } from 'wagmi';
import { bsc } from 'wagmi/chains';
import { useEffect } from 'react';
function App() {
const { chain, isConnected } = useAccount();
const { switchChain } = useSwitchChain();
useEffect(() => {
if (isConnected && chain?.id !== bsc.id) {
switchChain({ chainId: bsc.id });
}
}, [isConnected, chain?.id]);
// ...
}Wagmi config: put BSC firstThe first chain in the import { createConfig, http } from 'wagmi';
import { bsc, mainnet } from 'wagmi/chains';
const config = createConfig({
chains: [bsc, mainnet], // BSC first = default
transports: {
[bsc.id]: http(),
[mainnet.id]: http(),
},
});RainbowKit: set
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The following is the code for a project using React, Next.js, RainbowKit, and Wagmi.
`
import { getDefaultConfig, Chain } from '@rainbow-me/rainbowkit';
import {
metaMaskWallet,
okxWallet,
tokenPocketWallet,
trustWallet,
coinbaseWallet
} from '@rainbow-me/rainbowkit/wallets';
import { bsc, mainnet, bscTestnet, sepolia } from "viem/chains"
export const createWagmiConfig = getDefaultConfig({
appName: 'nffffeeee',
projectId: process.env.NEXT_PUBLIC_PRFDFDF || "" as string,
chains: [
bscTestnet,
sepolia,
],
wallets: [
{
groupName: 'Recommended',
wallets: [tokenPocketWallet, okxWallet, metaMaskWallet, trustWallet, coinbaseWallet],
},
],
multiInjectedProviderDiscovery: false,
ssr: true,
});
export const defaultChainId: number = bscTestnet.id;
export const defaultChain: Chain = bscTestnet as Chain; `
`function MyApp({ Component, pageProps }: AppPropsWithLayout) {
useSetViewportHeightCSS();
const router = useRouter();
const locale = router.locale || router.defaultLocale || 'zh';
const isZh = locale.startsWith('zh') && !locale.includes('Hant');
const isZhHant = locale.includes('Hant');
const langClass = isZh ? 'lang-zh' : (isZhHant ? 'lang-zh-Hant' : 'lang-en');
useEffect(() => {
if (typeof document !== 'undefined') {
const root = document.documentElement;
root.classList.remove('lang-zh', 'lang-en', 'lang-zh-Hant');
root.classList.add(langClass);
}
}, [langClass]);
useEffect(() => {
const designWidth = 640;
const maxWidth = 768;
const pcBaseFontSize = 16;
let rafId: number | null = null;
function refreshRem() {
const html = document.documentElement;
let width = html.clientWidth;
if (width >= 1024) {
html.style.fontSize = pcBaseFontSize + 'px';
return;
}
}, []);
const getLayout = Component.getLayout ?? ((page) => page);
return (
<>
);
}
export default MyApp;`
Beta Was this translation helpful? Give feedback.
All reactions