Skip to content

Commit

Permalink
fix: props로 넘기는 함수에 useCallback 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
wildcatco committed Aug 21, 2023
1 parent 9a5c1f2 commit 8e2bdb4
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/app/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function Provider({ children }: { children: React.ReactNode }) {
<JotaiProvider>
<BrowserRouter>{children}</BrowserRouter>
</JotaiProvider>
{/*<ReactQueryDevtools initialIsOpen={false} />*/}
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
);
}
1 change: 0 additions & 1 deletion src/features/posts/queries/useGetPost.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useQuery } from '@tanstack/react-query';
import { isNumber } from 'lodash';
import { axiosInstance } from '@/common/libs/axios';
import { GetPostResponse } from './dto/get-post';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function GenderSelect({ defaultValue, onChange }: GenderSelectProps) {
if (gender) {
onChange(gender);
}
}, [gender]);
}, [gender, onChange]);

return (
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AxiosError } from 'axios';
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { axiosInstance } from '@/common/libs/axios';
import { parseDateString } from '@/common/utils/date/parseDateString';
import { validateBirthDate } from '@/common/utils/validations/birth-date';
Expand Down Expand Up @@ -45,29 +45,35 @@ export function NicknameAgeGenderForm({
birthDate: null,
});

const handleNicknameChange = async (nickname: string) => {
setIsDirty({ ...isDirty, nickname: true });
setFormData({ ...formData, nickname });
const handleNicknameChange = useCallback(
async (nickname: string) => {
setIsDirty((prevIsDirty) => ({ ...prevIsDirty, nickname: true }));
setFormData((prevFormData) => ({ ...prevFormData, nickname }));

let error = validateNickname(nickname);
if (!error) {
try {
await axiosInstance.get(`/user/${user?.id}/validate-nickname`, {
params: { nickname },
});
} catch (e) {
if (e instanceof AxiosError && e.response?.status === 400) {
const errorCode = e.response.data.errorCode;
if (errorCode === 'IS_DUPLICATED_NICKNAME') {
error = '이미 존재하는 닉네임입니다.';
} else if (errorCode === 'IS_BAD_WORD') {
error = '금칙어 입력이 불가합니다.';
let error = validateNickname(nickname);
if (!error) {
try {
await axiosInstance.get(`/user/${user?.id}/validate-nickname`, {
params: { nickname },
});
} catch (e) {
if (e instanceof AxiosError && e.response?.status === 400) {
const errorCode = e.response.data.errorCode;
if (errorCode === 'IS_DUPLICATED_NICKNAME') {
error = '이미 존재하는 닉네임입니다.';
} else if (errorCode === 'IS_BAD_WORD') {
error = '금칙어 입력이 불가합니다.';
}
}
}
}
}
setErrorMessage({ ...errorMessage, nickname: error });
};
setErrorMessage((prevErrorMessage) => ({
...prevErrorMessage,
nickname: error,
}));
},
[user?.id],
);

const nicknameSuccessMessage =
isDirty.nickname && !errorMessage.nickname
Expand All @@ -76,21 +82,26 @@ export function NicknameAgeGenderForm({
const birthDateSuccessMessage =
isDirty.birthDate && !errorMessage.birthDate ? ' ' : null;

const handleBirthDateChange = ({ year, month, day }: BirthDate) => {
setIsDirty({ ...isDirty, birthDate: true });
setFormData({ ...formData, birthDate: `${year}-${month}-${day}` });

setErrorMessage({
...errorMessage,
birthDate: isDirty.birthDate
? validateBirthDate({ year, month, day })
: null,
});
};
const handleBirthDateChange = useCallback(
({ year, month, day }: BirthDate) => {
setIsDirty((prevIsDirty) => ({ ...prevIsDirty, birthDate: true }));
setFormData((prevFormData) => ({
...prevFormData,
birthDate: `${year}-${month}-${day}`,
}));
setErrorMessage((prevErrorMessage) => ({
...prevErrorMessage,
birthDate: isDirty.birthDate
? validateBirthDate({ year, month, day })
: null,
}));
},
[isDirty.birthDate],
);

const handleGenderChange = (gender: Gender) => {
setFormData({ ...formData, gender });
};
const handleGenderChange = useCallback((gender: Gender) => {
setFormData((prevFormData) => ({ ...prevFormData, gender }));
}, []);

useEffect(() => {
const isValid = Boolean(
Expand All @@ -101,7 +112,7 @@ export function NicknameAgeGenderForm({
!errorMessage.birthDate,
);
onChange(formData, isValid);
}, [formData, errorMessage]);
}, [formData, errorMessage, onChange]);

return (
<div className="space-y-[2.9rem]">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function RegionJobCategoryForm({
);

onChange(formData, isValid);
}, [formData, jobErrorMessage, isDirty]);
}, [formData, jobErrorMessage, isDirty, onChange]);

useEffect(() => {
if (formData.job) {
Expand Down
1 change: 0 additions & 1 deletion src/features/vote/components/VoteButton/VoteButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useAtom, useSetAtom } from 'jotai';
import { useEffect } from 'react';
import { FloatingButton } from '@/common/components/ui/buttons';
import { useUser } from '@/features/user/queries';
import { useChooseOption } from '@/features/vote/queries';
Expand Down
1 change: 0 additions & 1 deletion src/features/vote/components/VoteSection.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useAtomValue, useSetAtom } from 'jotai';
import { useAtom } from 'jotai/index';
import { useEffect } from 'react';
import { Spacing } from '@/common/components/ui';
import { getRemainingTime } from '@/features/posts/utils/get-remaining-time';
Expand Down
1 change: 0 additions & 1 deletion src/features/vote/queries/useGetChoiceOptions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useSuspenseQuery } from '@suspensive/react-query';
import { useQuery } from '@tanstack/react-query';
import { axiosInstance } from '@/common/libs/axios';
import { GetChoiceOptionsResponse } from './dto/get-choice-options';

Expand Down
1 change: 0 additions & 1 deletion src/features/vote/queries/useGetMyChoice.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useQuery } from '@tanstack/react-query';
import { isNumber } from 'lodash';
import { axiosInstance } from '@/common/libs/axios';
import { GetMyChoiceResponse } from './dto/get-my-choice';

Expand Down

0 comments on commit 8e2bdb4

Please sign in to comment.