Skip to content

Commit 8213d21

Browse files
authored
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.
1 parent dbc8ec8 commit 8213d21

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/pages/achievement/control/AchievementControl.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,18 @@ const AchievementControl: React.FC = () => {
3535
[dispatch]
3636
);
3737

38-
const inferencer = useTypedSelector(
39-
state => new AchievementInferencer(state.achievement.achievements, state.achievement.goals)
38+
// TODO: This is a hacky fix. By right, we shouldn't need to use an
39+
// inferencer instance since we can encapsulate the logic using hooks
40+
// and component state.
41+
const [initialAchievements, initialGoals] = useTypedSelector(state => [
42+
state.achievement.achievements,
43+
state.achievement.goals
44+
]);
45+
const inferencer = useMemo(
46+
() => new AchievementInferencer(initialAchievements, initialGoals),
47+
// We only want to create the inferencer once
48+
// eslint-disable-next-line react-hooks/exhaustive-deps
49+
[]
4050
);
4151

4252
/**

0 commit comments

Comments
 (0)