Skip to content

Commit 017b8f9

Browse files
committed
[YS-224] fix: Funnel 로 인해 의도치 않게 에러 상태에 따라 컴포넌트 자체를 갈아끼워 상태가 유지되지 않는 문제 해결
1 parent 372d659 commit 017b8f9

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

src/app/join/components/Participant/ParticipantForm.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const ParticipantForm = () => {
2020

2121
const { mutate: joinParticipant } = useParticipantJoinMutation();
2222

23-
const { Funnel, setStep } = useFunnel(['email', 'info', 'success'] as const);
23+
const { Funnel, Step, setStep } = useFunnel(['email', 'info', 'success'] as const);
2424

2525
const participantMethods = useForm<ParticipantJoinSchemaType>({
2626
resolver: zodResolver(ParticipantJoinSchema()),
@@ -61,17 +61,17 @@ const ParticipantForm = () => {
6161
return (
6262
<FormProvider {...participantMethods}>
6363
<Funnel>
64-
<Funnel.Step name={STEP.email}>
64+
<Step name={STEP.email}>
6565
<Participant.EmailStep onNext={() => setStep(STEP.info)} />
66-
</Funnel.Step>
67-
<Funnel.Step name={STEP.info}>
66+
</Step>
67+
<Step name={STEP.info}>
6868
<Participant.InfoStep
6969
handleSubmit={participantMethods.handleSubmit(handleParticipantSubmit)}
7070
/>
71-
</Funnel.Step>
72-
<Funnel.Step name={STEP.success}>
71+
</Step>
72+
<Step name={STEP.success}>
7373
<JoinSuccessStep name={participantMethods.getValues('name')} />
74-
</Funnel.Step>
74+
</Step>
7575
</Funnel>
7676
</FormProvider>
7777
);

src/app/join/components/Researcher/ResearcherForm.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const ResearcherForm = () => {
1818

1919
const { mutate: joinResearcher } = useResearcherJoinMutation();
2020

21-
const { Funnel, setStep } = useFunnel(['email', 'info', 'success'] as const);
21+
const { Funnel, Step, setStep } = useFunnel(['email', 'info', 'success'] as const);
2222

2323
const researcherMethods = useForm<ResearcherJoinSchemaType>({
2424
resolver: zodResolver(ResearcherJoinSchema()),
@@ -49,17 +49,17 @@ const ResearcherForm = () => {
4949
return (
5050
<FormProvider {...researcherMethods}>
5151
<Funnel>
52-
<Funnel.Step name={STEP.email}>
52+
<Step name={STEP.email}>
5353
<Researcher.EmailStep onNext={() => setStep(STEP.info)} />
54-
</Funnel.Step>
55-
<Funnel.Step name={STEP.info}>
54+
</Step>
55+
<Step name={STEP.info}>
5656
<Researcher.InfoStep
5757
handleSubmit={researcherMethods.handleSubmit(handleResearcherSubmit)}
5858
/>
59-
</Funnel.Step>
60-
<Funnel.Step name={STEP.success}>
59+
</Step>
60+
<Step name={STEP.success}>
6161
<JoinSuccessStep name={researcherMethods.getValues('name')} />
62-
</Funnel.Step>
62+
</Step>
6363
</Funnel>
6464
</FormProvider>
6565
);

src/app/join/hooks/useFunnel.tsx

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useRouter, useSearchParams } from 'next/navigation';
2-
import { ReactElement, ReactNode } from 'react';
2+
import { ReactElement, ReactNode, useMemo } from 'react';
33

44
const DEFAULT_STEP = 'email';
55

@@ -31,19 +31,25 @@ const useFunnel = <Steps extends StepsType>(steps: Steps) => {
3131
}
3232
};
3333

34-
const Funnel = ({ children }: FunnelProps) => {
35-
const targetStep = children.find((childStep) => childStep.props.name === currentStep);
34+
const Funnel = useMemo(
35+
() =>
36+
({ children }: FunnelProps) => {
37+
const targetStep = children.find((childStep) => childStep.props.name === currentStep);
3638

37-
return <>{targetStep}</>;
38-
};
39+
return <>{targetStep}</>;
40+
},
3941

40-
const Step = (props: StepProps) => {
41-
return <>{props.children}</>;
42-
};
42+
[currentStep],
43+
);
4344

44-
Funnel.Step = Step;
45+
const Step = useMemo(
46+
() => (props: StepProps) => {
47+
return <>{props.children}</>;
48+
},
49+
[],
50+
);
4551

46-
return { Funnel, setStep, step: currentStep } as const;
52+
return { Funnel, Step, setStep, step: currentStep } as const;
4753
};
4854

4955
export default useFunnel;

0 commit comments

Comments
 (0)