Hello I trying to register "Calendar" component from "Prime React" library with "React Form Hook". The problem is that I can’t set default value to the input.
This is where I set the default value.
const { register, handleSubmit, reset, formState: { errors }, control } = useForm({ mode: 'onSubmit', defaultValues: {
title: "test",
start: new Date(),
end: new Date()
} });
This is my component.
import React from 'react'
import { Calendar } from 'primereact/calendar';
import { useFormState } from 'react-hook-form';
import { CDatePickerStyled } from './CDatePicker.styled';
export default function CDatePicker({ required, disabled, register, control, placeHolder }) {
const { errors } = useFormState({ control });
return (
<CDatePickerStyled>
<Calendar className={errors[register.name] ? 'p-invalid' : ''} {...register} placeholder={placeHolder} disabled={disabled}></Calendar>
{required ? <span className={errors[register.name] ? 'error' : ''}>Required*</span> : null}
</CDatePickerStyled>
)
}
2
Answers
Isn’t that why there is a Calendar React Hook form example right on the documentation site: https://primereact.org/calendar/#hookform
use Controller like this: