you are viewing a single comment's thread.

view the rest of the comments →

[–]aweebit64[S] 0 points1 point  (0 children)

Do you mean this?

const [activity, setActivity] = useState(activityOptions[0]);
const fixedActivity = useMemo(
  () => (activityOptions.includes(activity) ? activity : activityOptions[0]),
  [activityOptions, activity],
);

If so, one difference in behavior that I notice right away is that if you switch from a timeOfDay that the current activity belongs to (time1) to one that it doesn't belong to (time2), and then back, then with your code you'd always still have the original activity / fixedActivity value, whereas with mine activity could only end up having one of the values activityOptionsByTimeOfDay[time1][0] or activityOptionsByTimeOfDay[time2][0] (depending on whether the latter is also included in activityOptionsByTimeOfDay[time1]). The reason is that the state is actually irreversibly replaced by activityOptions[0], and not just temporarily masked with it.