이상하게도 form 입력값들을 아무것도 건들지 않고 제출버튼을 누르면 handleSubmit 함수가 작동되지 않았다. (handleSubmit에는 validate 함수가 들어있는 상황)
알고보니 아래와 같은 에러들이 발생하고 있었고, 다음과 같이 해결하니 자연스럽게 해결되었다.
Warning: 'value' prop on 'input' should not be null. Consider using the empty string to clear the component or 'undefined' for uncontrolled components.
<input type="text" value={value || ''}/>
Warning: You provided a
checkedprop to a form field without anonChangehandler. This will render a read-only field. If the field should be mutable usedefaultChecked. Otherwise, set eitheronChangeorreadOnly.
onClick 핸들러와 checked 속성을 동시에 사용하면 안된다는 내용
<input type="radio" value={item} checked={field.value === item} onChange={field.onChange} />
// 전체 코드
const RadioButtonGroupRHF: React.FC<PropsType> = ({ radioList, name }) => {
const { control } = useFormContext();
return (
<Controller
control={control}
name={name}
render={({ field }) => (
<fieldset {...field}>
{radioList.map((item) => (
<label key={item}>
<input type="radio" value={item} checked={field.value === item} onChange={field.onChange} />
<span>{item}</span>
</label>
))}
</fieldset>
)}
/>
);
};
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
React Hook Form과 같이 ref를 기반으로 동작하는 라이브러리를 사용한다면 고려할 수 있는 문제
react-hook-form이 ref를 자식컴포넌트에 전달하기 때문에 발생한 에러
react props는 ref를 전달하지 못한다
const Input = forwardRef((props, ref) => { return <input ref={ref} />; });
cotroller (ref 공유)
https://stackoverflow.com/questions/67877887/react-hook-form-v7-function-components-cannot-be-given-refs-attempts-to-access
https://react-hook-form.com/faqs
내가 사용한 라이브러리는 ... field를 전체 스프레드하지 않고 필요한 것만 사용하는 것으로 변경했다.
// 전체 코드
import Tags from "@yaireo/tagify/dist/react.tagify";
import { Controller, useFormContext } from "react-hook-form";
const TagInputRHF = () => {
const { control } = useFormContext();
return (
<Controller
control={control}
name="tags"
render={({ field }) => (
<Tags
// {...field}
defaultValue={field.value}
onChange={(e) => {
const values = e.detail.tagify.value.map((item) => item.value);
field.onChange(values);
}}
/>
)}
/>
);
};