Yup useForm setValue 사용

이경은·2023년 3월 14일
0

📌 setValue

Yup useForm을 사용할 경우, 입력폼 외에서 값을 변경하고 싶을 때에는 setValue method를 사용하면 된다. 아래처럼 methods로 선언하고, 아래에서 key, value 형태로 변경하면 된다.

const UpdateDeviceSchema = Yup.object().shape({
  displayName: Yup.string().required('Display name is required'),
	// ...
});

const defaultValues = {
  displayName: '',
	// ...
};

const methods = useForm({
  resolver: yupResolver(UpdateDeviceSchema),
  defaultValues,
});

const {
  reset,
  setError,
  setValue,
  handleSubmit,
  formState: { isSubmitting },
} = methods;

// ...

// setValue(key, value) 형태로 값 설정.
setValue('displayName', info.displayName);

참조
https://react-hook-form.com/api/useform/setvalue/

profile
Web Developer

0개의 댓글