From a4e9d048e911a857756fe84c433c834f517e1d5b Mon Sep 17 00:00:00 2001 From: Akshay Shekhawat Date: Mon, 22 Aug 2022 12:39:06 -0700 Subject: [PATCH] (fix) infinite render loop on test page --- .../components/Authentication/authSwitch.tsx | 88 +++++++++---------- .../components/TestEditor/requestEditor.tsx | 9 +- 2 files changed, 46 insertions(+), 51 deletions(-) diff --git a/frontend/src/components/Authentication/authSwitch.tsx b/frontend/src/components/Authentication/authSwitch.tsx index f6996002..cd9f0425 100644 --- a/frontend/src/components/Authentication/authSwitch.tsx +++ b/frontend/src/components/Authentication/authSwitch.tsx @@ -1,6 +1,7 @@ import { Box, HStack, Select, StackDivider, VStack } from "@chakra-ui/react"; import { AuthType } from "@common/testing/enums"; import { Authorization, Request } from "@common/testing/types"; +import React, { useCallback } from "react"; import APIAuth from "./apiKey"; import BasicAuth from "./basicAuth"; import BearerAuth from "./bearerAuth"; @@ -9,50 +10,49 @@ import NoAuth from "./noAuth"; interface AuthSwitchInterface { variant: AuthType; setVariant: (value: AuthType) => void; - request: Request; - setRequest: (v: Request) => void; + setRequest: (t: (e: Request) => Request) => void; } -const AuthSwitch: React.FC = ({ - variant, - setVariant, - request, - setRequest, -}) => { - const onAuthParamsChange = (v: () => Authorization) => { - setRequest({ ...request, authorization: v() }); - }; - const getAuthComponent = (auth: AuthType) => { - switch (auth) { - case AuthType.API_KEY: - return ; - case AuthType.BASIC_AUTH: - return ; - case AuthType.NO_AUTH: - return ; - case AuthType.BEARER: - return ; - } - }; - return ( - }> - - Type - - - - - {getAuthComponent(variant)} - - ); -}; +const AuthSwitch: React.FC = React.memo( + ({ variant, setVariant, setRequest }) => { + const onAuthParamsChange = useCallback( + (v: () => Authorization) => { + setRequest((request) => ({ ...request, authorization: v() })); + }, + [setRequest] + ); + const getAuthComponent = (auth: AuthType) => { + switch (auth) { + case AuthType.API_KEY: + return ; + case AuthType.BASIC_AUTH: + return ; + case AuthType.NO_AUTH: + return ; + case AuthType.BEARER: + return ; + } + }; + return ( + }> + + Type + + + + + {getAuthComponent(variant)} + + ); + } +); export default AuthSwitch; diff --git a/frontend/src/components/TestEditor/requestEditor.tsx b/frontend/src/components/TestEditor/requestEditor.tsx index 3b138fa1..7c37fa26 100644 --- a/frontend/src/components/TestEditor/requestEditor.tsx +++ b/frontend/src/components/TestEditor/requestEditor.tsx @@ -125,16 +125,11 @@ const RequestEditor: React.FC = React.memo( /> - + { - updateRequest((req) => { - return { ...req, ...updatedReq }; - }); - }} + setRequest={updateRequest} />