From 26251e1e604f8615a15329d52b4ee7b6479aaa9b Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Tue, 31 May 2022 10:33:10 -0400 Subject: [PATCH] Don't abuse useMemo for creating a MatrixClient --- src/auth/useInteractiveRegistration.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/auth/useInteractiveRegistration.ts b/src/auth/useInteractiveRegistration.ts index f58c9f0..2386b53 100644 --- a/src/auth/useInteractiveRegistration.ts +++ b/src/auth/useInteractiveRegistration.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { useState, useEffect, useCallback, useMemo } from "react"; +import { useState, useEffect, useCallback, useRef } from "react"; import matrix, { InteractiveAuth } from "matrix-js-sdk/src/browser-index"; import { MatrixClient } from "matrix-js-sdk/src/client"; @@ -35,16 +35,19 @@ export const useInteractiveRegistration = (): [ const [privacyPolicyUrl, setPrivacyPolicyUrl] = useState(); const [recaptchaKey, setRecaptchaKey] = useState(); - const authClient = useMemo(() => matrix.createClient(defaultHomeserver), []); + const authClient = useRef(); + if (!authClient.current) { + authClient.current = matrix.createClient(defaultHomeserver); + } useEffect(() => { - authClient.registerRequest({}).catch((error) => { + authClient.current.registerRequest({}).catch((error) => { setPrivacyPolicyUrl( error.data?.params["m.login.terms"]?.policies?.privacy_policy?.en?.url ); setRecaptchaKey(error.data?.params["m.login.recaptcha"]?.public_key); }); - }, [authClient]); + }, []); const register = useCallback( async ( @@ -55,9 +58,9 @@ export const useInteractiveRegistration = (): [ passwordlessUser?: boolean ): Promise<[MatrixClient, Session]> => { const interactiveAuth = new InteractiveAuth({ - matrixClient: authClient, + matrixClient: authClient.current, doRequest: (auth) => - authClient.registerRequest({ + authClient.current.registerRequest({ username, password, auth: auth || undefined, @@ -111,7 +114,7 @@ export const useInteractiveRegistration = (): [ return [client, session]; }, - [authClient] + [] ); return [privacyPolicyUrl, recaptchaKey, register];