-
react-hook-form Controller를 이용하여 재사용 컴포넌트를 만들때 control props 전달 타입체커 환경 에러React.js/React-hook-form 2024. 5. 11. 20:15728x90
에러 :Type 'Control<MyFormValues>' is not assignable to type 'Control<FieldValues>'
방법1 as unknown as 문법을 이용한 타입 단언
https://github.com/react-hook-form/react-hook-form/issues/4965#issuecomment-826862827
Type 'Control<MyFormValues>' is not assignable to type 'Control<FieldValues>' · Issue #4965 · react-hook-form/react-hook-form
This seems like a duplicate of #2984. Describe the bug I am experiencing this issue after following the Control example in your documentation and using any 7.x release. (have tried 7.0, 7.1, 7.2, 7...
github.com
방법2 제네릭을 이용한 props type 선언
https://github.com/react-hook-form/react-hook-form/issues/4965#issuecomment-1243612602
Type 'Control<MyFormValues>' is not assignable to type 'Control<FieldValues>' · Issue #4965 · react-hook-form/react-hook-form
This seems like a duplicate of #2984. Describe the bug I am experiencing this issue after following the Control example in your documentation and using any 7.x release. (have tried 7.0, 7.1, 7.2, 7...
github.com
방법2로 하는게 as unknwon으로 강제로 타입 단언 하는것보다 좋은것 같아서 방법2로 해결
// MentionField.tsx import React, { FC } from 'react'; import { Mention, MentionsInput } from 'react-mentions'; import style from './MentionField.module.scss'; import { Control, Controller, Path, RegisterOptions } from 'react-hook-form'; import { MemberService } from '@/services/member/member.service'; import { useQuery } from 'react-query'; interface Props<T extends Record<string, any>> { control: Control<T>; fieldName: Path<T>; validationOptions?: Omit< RegisterOptions<T, Path<T>>, 'valueAsNumber' | 'valueAsDate' | 'setValueAs' | 'disabled' >; } const MentionField = <T extends Record<string, any>>(props: Props<T>) => { const { fieldName, control, validationOptions } = props; ... 중략 ... }; export default MentionField; // 부모 컴포넌트 <MentionField control={control} // good! fieldName="commentContents" validationOptions={{ required: '필수입니다', }} ></MentionField>
728x90'React.js > React-hook-form' 카테고리의 다른 글
watch 메서드를 활용하여 감시하기 (0) 2024.07.30 react-hook-form에서 useController 커스텀 훅 사용 (0) 2024.06.25 react-hook-form Controller 사용시 rules validate가 동작하지 않을때 (0) 2024.05.11 react-hook-form과 다른 라이브러와 함께 사용 (0) 2024.05.02 react-hook-form의 getValues와 setValue를 이용하여 커스텀 훅을 만들때 Path<T> 에러 (0) 2023.10.30