Skip to content

A local-first personal finance app

Fix react-hooks/exhaustive-deps in DateSelect (#6864)

Joel Jeremy Marquezc0bd920c264e
diff --git a/packages/desktop-client/src/components/select/DateSelect.tsx b/packages/desktop-client/src/components/select/DateSelect.tsx
index e975271031..d1a62a8a6f 100644
--- a/packages/desktop-client/src/components/select/DateSelect.tsx
+++ b/packages/desktop-client/src/components/select/DateSelect.tsx
@@ -2,6 +2,7 @@
import React, {
forwardRef,
useEffect,
+ useEffectEvent,
useImperativeHandle,
useLayoutEffect,
useMemo,
@@ -142,6 +143,8 @@ const DatePicker = forwardRef<DatePickerForwardedRef, DatePickerProps>(
const picker = useRef(null);
const mountPoint = useRef(null);
+ const onUpdateEffect = useEffectEvent(onUpdate);
+
useImperativeHandle(
ref,
() => ({
@@ -169,18 +172,16 @@ const DatePicker = forwardRef<DatePickerForwardedRef, DatePickerProps>(
if (newDate) {
picker.current.setDate(newDate, true);
- onUpdate?.(newDate);
+ onUpdateEffect?.(newDate);
}
},
}),
- // oxlint-disable-next-line react-hooks/exhaustive-deps
[],
);
- useLayoutEffect(() => {
+ const initPikaday = useEffectEvent(() => {
const pikadayLocale = createPikadayLocale(locale);
-
- picker.current = new Pikaday({
+ return new Pikaday({
theme: 'actual-date-picker',
keyboardInput: false,
firstDay: parseInt(firstDayOfWeekIdx),
@@ -197,13 +198,15 @@ const DatePicker = forwardRef<DatePickerForwardedRef, DatePickerProps>(
onSelect,
i18n: pikadayLocale,
});
+ });
+ useLayoutEffect(() => {
+ picker.current = initPikaday();
mountPoint.current.appendChild(picker.current.el);
return () => {
picker.current.destroy();
};
- // oxlint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
@@ -274,51 +277,43 @@ function DateSelectDesktop({
const innerRef = useRef<HTMLInputElement | null>(null);
const mergedRef = useMergedRefs<HTMLInputElement>(innerRef, ref);
- // This is confusing, so let me explain: `selectedValue` should be
- // renamed to `currentValue`. It represents the current highlighted
- // value in the date select and always changes as the user moves
- // around. `userSelectedValue` represents the last value that the
- // user actually selected (with enter or click). Having both allows
- // us to make various UX decisions
const [selectedValue, setSelectedValue] = useState(value);
- const userSelectedValue = useRef(selectedValue);
const [_firstDayOfWeekIdx] = useSyncedPref('firstDayOfWeekIdx');
const firstDayOfWeekIdx = _firstDayOfWeekIdx || '0';
- useEffect(() => {
- userSelectedValue.current = value;
- }, [value]);
-
useEffect(() => setValue(parsedDefaultValue), [parsedDefaultValue]);
- useEffect(() => {
- if (getDayMonthRegex(dateFormat).test(value)) {
+ const onUpdateEffect = useEffectEvent((newValue: string) => {
+ if (getDayMonthRegex(dateFormat).test(newValue)) {
// Support only entering the month and day (4/5). This is complex
// because of the various date formats - we need to derive
// the right day/month format from it
- const test = parse(value, getDayMonthFormat(dateFormat), new Date());
+ const test = parse(newValue, getDayMonthFormat(dateFormat), new Date());
if (isValid(test)) {
onUpdate?.(format(test, 'yyyy-MM-dd'));
setSelectedValue(format(test, dateFormat));
}
- } else if (getShortYearRegex(dateFormat).test(value)) {
+ } else if (getShortYearRegex(dateFormat).test(newValue)) {
// Support entering the year as only two digits (4/5/19)
- const test = parse(value, getShortYearFormat(dateFormat), new Date());
+ const test = parse(newValue, getShortYearFormat(dateFormat), new Date());
if (isValid(test)) {
onUpdate?.(format(test, 'yyyy-MM-dd'));
setSelectedValue(format(test, dateFormat));
}
} else {
- const test = parse(value, dateFormat, new Date());
+ const test = parse(newValue, dateFormat, new Date());
if (isValid(test)) {
const date = format(test, 'yyyy-MM-dd');
onUpdate?.(date);
- setSelectedValue(value);
+ setSelectedValue(newValue);
}
}
- // oxlint-disable-next-line react-hooks/exhaustive-deps
- }, [value, dateFormat]);
+ });
+
+ useEffect(() => {
+ onUpdateEffect(value);
+ }, [value]);
function onKeyDown(e: KeyboardEvent<HTMLInputElement>) {
if (
diff --git a/upcoming-release-notes/6864.md b/upcoming-release-notes/6864.md
new file mode 100644
index 0000000000..c7e31d2e3f
--- /dev/null
+++ b/upcoming-release-notes/6864.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [joel-jeremy]
+---
+
+Fix react-hooks/exhaustive-deps in DateSelect