Skip to content

Commit c3c0386

Browse files
committed
fix(nextjs-shadcn): check network before switching
1 parent d4e8667 commit c3c0386

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

examples/nextjs-shadcn/src/app/embeds.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
sendTransaction,
1919
switchNetwork,
2020
waitForTransaction,
21+
getNetwork,
2122
} from "@wagmi/core";
2223
import { useMemo } from "react";
2324
import { useAccount } from "wagmi";
@@ -41,7 +42,10 @@ export function Embeds(props: { embeds: Array<Embed> }) {
4142
const parsedChainId = parseInt(chainId);
4243

4344
// Switch chains if the user is not on the right one
44-
await switchNetwork({ chainId: parsedChainId });
45+
// Note: silently fails if switching to the same chain
46+
const network = getNetwork();
47+
if (network.chain.id !== parsedChainId)
48+
await switchNetwork({ chainId: parsedChainId });
4549

4650
// Send the transaction
4751
const { hash } = await sendTransaction({
@@ -58,6 +62,7 @@ export function Embeds(props: { embeds: Array<Embed> }) {
5862

5963
onConfirmed(hash, status === "success");
6064
} catch (e) {
65+
console.error(e);
6166
onError(e);
6267
}
6368
},

0 commit comments

Comments
 (0)