From 8213d21951b01a27861bcb684d7981d52364bcfa Mon Sep 17 00:00:00 2001 From: Richard Dominick <34370238+RichDom2185@users.noreply.github.com> Date: Tue, 8 Oct 2024 18:22:43 +0800 Subject: [PATCH] fix: Memoize achievement inferencer (#3061) Uses a per-component instance singleton and memoizes it, as we only need to keep track of the initial states of the control panel. Also added a TODO comment for future refactoring to more modern React principles using component state and hooks. --- .../achievement/control/AchievementControl.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/pages/achievement/control/AchievementControl.tsx b/src/pages/achievement/control/AchievementControl.tsx index 1f464c2bdf..caf60cfa54 100644 --- a/src/pages/achievement/control/AchievementControl.tsx +++ b/src/pages/achievement/control/AchievementControl.tsx @@ -35,8 +35,18 @@ const AchievementControl: React.FC = () => { [dispatch] ); - const inferencer = useTypedSelector( - state => new AchievementInferencer(state.achievement.achievements, state.achievement.goals) + // TODO: This is a hacky fix. By right, we shouldn't need to use an + // inferencer instance since we can encapsulate the logic using hooks + // and component state. + const [initialAchievements, initialGoals] = useTypedSelector(state => [ + state.achievement.achievements, + state.achievement.goals + ]); + const inferencer = useMemo( + () => new AchievementInferencer(initialAchievements, initialGoals), + // We only want to create the inferencer once + // eslint-disable-next-line react-hooks/exhaustive-deps + [] ); /**