Skip to content

Commit

Permalink
fix: FormattedDate useEffect/initial state bug (#4383)
Browse files Browse the repository at this point in the history
Refactor to initialize the dateObject from state instead of relying on useEffect
  • Loading branch information
dsamojlenko authored Oct 11, 2024
1 parent 1570884 commit b3e3875
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions components/clientComponents/forms/FormattedDate/FormattedDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export const FormattedDate = (props: FormattedDateProps): React.ReactElement =>
autocomplete = false,
} = props;

const [dateObject, setDateObject] = useState<DateObject | null>(null);
const [field, meta, helpers] = useField(props);
const [dateObject, setDateObject] = useState<DateObject | null>(
field.value ? JSON.parse(field.value) : null
);
const { t } = useTranslation("common");

let dateFormat = initialDateFormat;
Expand All @@ -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) {
Expand Down

0 comments on commit b3e3875

Please sign in to comment.