Skip to content

Commit

Permalink
Refactor the use of hooks
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Kumar <[email protected]>
  • Loading branch information
abhi-kr-2100 committed May 19, 2024
1 parent 2796198 commit 6a09dcd
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions webapp/src/PlayPage/Word.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useEffect, useMemo, useState } from 'react';
import {
DependencyList,
Dispatch,
SetStateAction,
useEffect,
useState,
} from 'react';
import {
Box,
Button,
Expand Down Expand Up @@ -56,7 +62,9 @@ function WordPopover({
const { t } = useTranslation('WordDifficulty');
const { t: tc } = useTranslation('common');

const [ef, setEF] = useState(wordScore.score.easinessFactor);
const [ef, setEF] = useSyncedState(wordScore.score.easinessFactor, [
wordScore.score.easinessFactor,
]);

const MIN_EF = 1.3;
const DEFAULT_EF = 2.5;
Expand All @@ -69,23 +77,20 @@ function WordPopover({
setParentEF(ef);
};

const marks = useMemo(
() => [
{
value: MIN_EF,
label: t('Difficult'),
},
{
value: DEFAULT_EF,
label: t('Neutral'),
},
{
value: MAX_EF,
label: t('Easy'),
},
],
[wordScore.score.easinessFactor],
);
const marks = [
{
value: MIN_EF,
label: t('Difficult'),
},
{
value: DEFAULT_EF,
label: t('Neutral'),
},
{
value: MAX_EF,
label: t('Easy'),
},
];

return (
<Popover {...PopoverProps}>
Expand Down Expand Up @@ -165,6 +170,16 @@ function useAnchorEl() {
};
}

function useSyncedState<T>(
initial: T,
dependencies: DependencyList,
): [T, Dispatch<SetStateAction<T>>] {
const [state, setState] = useState(initial);
useEffect(() => setState(dependencies[0] as T), dependencies);

return [state, setState];
}

interface WordPopoverProps {
wordScore: CorrectedWordScoreType;
setEF: (ef: number) => void;
Expand Down

0 comments on commit 6a09dcd

Please sign in to comment.