@@ -3,7 +3,8 @@ import { LoadingSpinnerButton } from '@/components/button';
33import { InputField } from '@/components/input' ;
44import { MAX_NICKNAME_LENGTH , MIN_NICKNAME_LENGTH , NICKNAME_REGEX } from '@/constants' ;
55import { useMutation } from '@tanstack/react-query' ;
6- import { useEffect , useState } from 'react' ;
6+ import { throttle } from 'lodash' ;
7+ import { useEffect , useMemo , useState } from 'react' ;
78
89interface NicknameInputProps {
910 initialText ?: string ; // 초기값
@@ -42,7 +43,7 @@ function NicknameInput({ initialText = '', setValidity }: NicknameInputProps) {
4243 } ;
4344 // 닉네임 중복을 확인하는 함수
4445 const { mutate, isPending } = useMutation ( {
45- mutationFn : ( ) => getNicknameAvailability ( text ) ,
46+ mutationFn : ( value : string ) => getNicknameAvailability ( value ) ,
4647 onSuccess : ( data ) => {
4748 if ( data . code === 200 ) {
4849 setValidationStatus ( { isValid : true , message : '사용 가능한 닉네임입니다' } ) ;
@@ -64,6 +65,8 @@ function NicknameInput({ initialText = '', setValidity }: NicknameInputProps) {
6465 setText ( initialText ) ;
6566 } , [ initialText ] ) ;
6667
68+ const throttledMutate = useMemo ( ( ) => throttle ( ( value : string ) => mutate ( value ) , 1000 ) , [ mutate ] ) ;
69+
6770 return (
6871 < InputField
6972 id = "nickname"
@@ -80,7 +83,7 @@ function NicknameInput({ initialText = '', setValidity }: NicknameInputProps) {
8083 className = "w-[65px]"
8184 text = "중복확인"
8285 disabled = { ! validationStatus . isValid }
83- onClick = { ( ) => mutate ( ) }
86+ onClick = { ( ) => throttledMutate ( text ) }
8487 />
8588 }
8689 />
0 commit comments