Open rsmpark opened 9 months ago
const CountdownPrompt = () => {
const [count, setCount] = useState(7);
const { dispatch } = useAppCtx();
const { state } = useMetronomeCtx();
const metronomeCnt = state.count;
const didMount = useRef(false);
useEffect(() => {
if (didMount.current) {
setCount((prevCount) => prevCount - 1);
}
didMount.current = true;
}, [metronomeCnt]);
if (count === 0) {
dispatch({ type: "NEXT_STAGE" });
}
return <div>{<p>{count}</p>}</div>;
};
export default CountdownPrompt;
Countdown count is synchronized with the metronome count state. -> if metronome count changes, decrement countdown
However, this causes issues as useEffect
is run twice causing the countdown to be decremented even if metronome count is not updated
Need to explore alternative approaches. Possible solutions:
Use RTK store
Create custom middleware for Context API
createListenerMiddleware
Create behaviour composition on useMetronome
hooks
useMetronome
hook to extend its behaviour to allow synchronization with CountdownPrompt
The countdown should be in sync with the click of the metronome.