Skip to content

Conversation

@jsk3342
Copy link
Collaborator

@jsk3342 jsk3342 commented Nov 21, 2024

🚀 Issue Number

📝 주요 작업

1. React Hook Form 도입

  • 기존 useState로 관리되던 폼 상태를 React Hook Form으로 마이그레이션
  • FormProvider를 사용하여 폼 컨텍스트 관리 구조화
  • 각 필드를 개별 컴포넌트로 분리하여 재사용성 향상

2. 성능 최적화

  • Uncontrolled 컴포넌트 패턴 적용으로 불필요한 리렌더링 방지
  • 필드별 상태 격리로 독립적인 업데이트 구현

고민과 해결 과정

as-is

최적화 전 렌더링

// ... 각 필드마다 개별 상태 관리
const [liveTitle, setLiveTitle] = useState('');
const [category, setCategory] = useState('');
  • 모든 입력 필드가 상태 변경시마다 리렌더링 발생
  • 불필요한 상태 업데이트로 인한 성능 저하

to-be

최적화 후

const methods = useForm<FormValues>({
  defaultValues: {
    liveTitle: '',
    category: '',
    tag: '',
    tags: [],
    notice: '',
    previewImage: null
  }
});
  • 변경된 필드만 선택적으로 리렌더링
  • React Hook Form의 Controller 컴포넌트를 통한 효율적인 상태 관리

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type폴더에 추가하지 않고 Form폴더 안에서 type을 지정하신 것은 여기서만 사용해서 그런걸까요? + 폴더간 물리적 거리가 멀어서 그런걸까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api에서도 동일 타입을 사용하고 있어서 분리하는게 좋아보이네요 :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

긴 내용의 style코드를 분리한 것이 좋습니다!

<Button onClick={handleUpdate}>업데이트</Button>
</FormArea>
</Container>
<FormProvider {...methods}>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FormProvider로 useFormContext를 사용한것이 인상적입니다!!
그런데 계층이 한단계인데 context를 사용한 이유가 있을까요?

if (currentTag) {
const currentTags = getValues('tags');
setValue('tags', [...currentTags, currentTag]);
setValue('tag', '');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setValue가 2개인데 의도한 것인가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tag, tags가 있는데 tag에서 엔터를 치면 tags 배열에 들어가고 tag는 다른 인풋을 받기 위해 빈스트링으로 설정합니다.

@spearStr
Copy link
Collaborator

react-hook-form 라이브러리를 사용하여 효율적으로 form관리를 한 것이 인상깊었습니다!

@spearStr spearStr merged commit 1c8ba06 into dev-fe Nov 21, 2024
2 checks passed
@spearStr spearStr deleted the feature-FE-#123-host_hook_form branch November 21, 2024 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

FE🎄 FEAT 기능 구현

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants