From 1f11e283d7134f469d011590ac315c2f78d688ce Mon Sep 17 00:00:00 2001 From: shreerang6921 <68148922+shreerang6921@users.noreply.github.com> Date: Wed, 24 Apr 2024 17:59:55 +0530 Subject: [PATCH] Prevent double initialization of wallet connect client (#10) --- src/context/WalletConnectContext.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/context/WalletConnectContext.tsx b/src/context/WalletConnectContext.tsx index 027b95d..8673624 100644 --- a/src/context/WalletConnectContext.tsx +++ b/src/context/WalletConnectContext.tsx @@ -6,6 +6,7 @@ import React, { useState, useEffect, useCallback, + useRef, } from "react"; import { SnackbarProvider, enqueueSnackbar } from "notistack"; import { useNavigate } from "react-router-dom"; @@ -46,6 +47,7 @@ export const WalletConnectProvider = ({ }) => { const [signClient, setSignClient] = useState(); const [session, setSession] = useState(null); + const isSignClientInitializing = useRef(false); const navigate = useNavigate() const disconnect = useCallback(async () => { @@ -85,6 +87,7 @@ export const WalletConnectProvider = ({ ); const createClient = useCallback(async () => { + isSignClientInitializing.current = true; const signClient = await SignClient.init({ projectId: PROJECT_ID, metadata: { @@ -98,6 +101,7 @@ export const WalletConnectProvider = ({ setSignClient(signClient); await subscribeToEvents(signClient); await checkPersistedState(signClient); + isSignClientInitializing.current = false; }, [checkPersistedState, subscribeToEvents]) const connect = async () => { @@ -136,7 +140,7 @@ export const WalletConnectProvider = ({ }; useEffect(() => { - if (!signClient) { + if (!signClient && !isSignClientInitializing.current) { createClient(); } }, [signClient, createClient]);