diff --git a/components/clientComponents/forms/FormattedDate/FormattedDate.tsx b/components/clientComponents/forms/FormattedDate/FormattedDate.tsx index 423b4d7a3a..3b960cdb2d 100644 --- a/components/clientComponents/forms/FormattedDate/FormattedDate.tsx +++ b/components/clientComponents/forms/FormattedDate/FormattedDate.tsx @@ -30,8 +30,10 @@ export const FormattedDate = (props: FormattedDateProps): React.ReactElement => autocomplete = false, } = props; - const [dateObject, setDateObject] = useState(null); const [field, meta, helpers] = useField(props); + const [dateObject, setDateObject] = useState( + field.value ? JSON.parse(field.value) : null + ); const { t } = useTranslation("common"); let dateFormat = initialDateFormat; @@ -55,18 +57,6 @@ export const FormattedDate = (props: FormattedDateProps): React.ReactElement => } }); - // Update the date object when the field value changes - useEffect(() => { - if (field.value) { - try { - const parsedValue = JSON.parse(field.value); - setDateObject(parsedValue); - } catch (e) { - setDateObject(null); - } - } - }, [field.value]); - // Update the field value when the date object changes useEffect(() => { if (dateObject) {