import { useEffect, useRef } from 'react'; import { useUnit, useEvent } from…
- User
import { useEffect, useRef } from 'react'; import { useUnit, useEvent } from 'effector-react';
import { WordInput } from '@/features/input-word/ui/word-input/index.jsx'; import { iterateReveal, startGame, startRevealing, stopRevealing, } from '@/pages/game-page/model/events.js'; import {isRevealing} from '../model/store.js'; import { ProgressBar } from '@/shared/ui/progress-bar'; import * as styles from './game-page.module.css';
export const GamePage = () => { const progressBarRef = useRef(null); const isRevealing = useUnit(canReveal); const [triggerStartRevealing, triggerStopRevealing, triggerReveal] = useUnit([startRevealing, stopRevealing, iterateReveal]) const triggerStartGame = useUnit(startGame);
useEffect(() => { triggerStartGame(); }, []);
useEffect(() => { if(!isRevealing) return; progressBarRef.current.start(); }, [isRevealing]);
const onProgressEnd = () => { if(!canReveal) return; triggerReveal(); progressBarRef.current.start(); }
return ( ); }; import {createStore, sample} from "effector"; import {and, or, some} from "patronum";
import {fetchWordFx, setWord} from "@/entites/word"; import {initUserGuess, revealRandomSymbol} from "@/features/input-word/model/events.js"; import {$userGuess} from "@/features/input-word/model/store.js"; import {iterateReveal, startGame, startRevealing, stopRevealing} from "@/pages/game-page/model/events.js";
export const hasUnrevealedSymbol = canReveal = and(hasUnrevealedSymbol);
isRevealing.on(stopRevealing, () => false);
iterateReveal.watch(() => { console.log(userGuess.getState(), $isRevealing.getState()); });
sample({ clock: startGame, target: fetchWordFx, });
sample({ clock: setWord, fn: word => word.length, target: initUserGuess, });
sample({ clock: initUserGuess, target: startRevealing, });
sample({ clock: iterateReveal, target: revealRandomSymbol, });
export {}; после открытия все символов progressBar заполняется еще раз
- Assistant
It seems like you're experiencing an issue where the progress bar fills up once more even after all symbols have been revealed. This behavior is unexpected because
canRevealshould becomefalsewhen there are no more symbols to reveal, preventing the progress bar from restarting.The root cause of this issue lies in how the
canRevealvalue is accessed within theonProgressEndhandler. In React, when you define functions inside your component (likeonProgressEnd), they capture the values of variables from the render scope where they were defined. If these variables change without causing a re-render, the functions will still hold onto the old values. This is often referred to as a "stale closure" problem.In your case, the
canRevealvariable insideonProgressEnddoesn't get updated because it refers to the value captured during the component's initial render. When all symbols are revealed,$canRevealbecomesfalse, but theonProgressEndfunction still has the oldtruevalue, causing it to restart the progress bar when it shouldn't.Solution:
To fix this issue, you need to ensure that
onProgressEndalways uses the latest value ofcanRevealwhen it's called. You can achieve this by directly accessing the current state of$canRevealfrom Effector's store within theonProgressEndfunction.Here's how you can modify your
GamePagecomponent:import { useEffect, useRef } from 'react'; import { useUnit } from 'effector-react'; import { WordInput } from '@/features/input-word/ui/word-input/index.jsx'; import { iterateReveal, startGame, startRevealing, stopRevealing, } from '@/pages/game-page/model/events.js'; import { $isRevealing, $canReveal } from '../model/store.js'; import { ProgressBar } from '@/shared/ui/progress-bar'; import * as styles from './game-page.module.css'; export const GamePage = () => { const progressBarRef = useRef(null); const isRevealing = useUnit($isRevealing); const [triggerStartRevealing, triggerStopRevealing, triggerReveal] = useUnit([ startRevealing, stopRevealing, iterateReveal, ]); const triggerStartGame = useUnit(startGame); useEffect(() => { triggerStartGame(); }, []); useEffect(() => { if (!isRevealing) return; progressBarRef.current.start(); }, [isRevealing]); const onProgressEnd = () => { // Get the latest value of canReveal from the store const canReveal = $canReveal.getState(); if (!canReveal) return; triggerReveal(); progressBarRef.current.start(); }; return ( <div className={styles.page}> <WordInput /> <ProgressBar ref={progressBarRef} fillingDuration={1000} onProgressEnd={onProgressEnd} /> </div> ); };Explanation:
- Accessing the Latest State: By calling
$canReveal.getState()insideonProgressEnd, you access the most recent value of$canRevealfrom the Effector store at the very moment the function is executed. - Avoiding Stale Closures: This approach prevents the stale closure issue because you're not relying on the
canRevealvariable from the component's render scope, which might be outdated. - Conditional Logic Works Correctly: Now, when all symbols are revealed and
$canRevealbecomesfalse, theonProgressEndfunction will see this latest value and prevent the progress bar from starting again.
Additional Notes:
- Effector Store Access: It's generally acceptable to access the store's current state using
getState()when you need the latest value in an event handler likeonProgressEnd. - Avoid Over-Relying on Hooks for Latest State in Handlers: While hooks like
useUnitanduseStoreare great for managing reactive values during render, they don't help with variables inside event handlers due to closure scopes. - Clean Code Practices: Ensure that you import
$canRevealdirectly in your component to access it. This keeps your code clean and maintainable.
Conclusion:
By updating
onProgressEndto fetch the latest value ofcanReveal, you resolve the issue where the progress bar fills up again unnecessarily. This ensures that the progress bar behaves correctly, starting only when there are symbols left to reveal and stopping appropriately when all symbols have been revealed. - Accessing the Latest State: By calling