N
N
Nikita2019-11-09 00:12:00
React
Nikita, 2019-11-09 00:12:00

What is the correct way to change useState with setInterval?

Tell me how to change the state in a functional component?
It is necessary that when the button is clicked, setInterval is launched, which changes the state every second, but the state does not change correctly when using setInterval.
I tried to install libraries, but everything ended with errors.

const [timePass, setTimePass] = useState<number>(currentLvl);
const [currentLvl] = useState<number>(difficultyLevels.easy);

 const onChangeHandler = (event: { target: { value: any; }; }) => {

        setInputValue(event.target.value);

        if (randomWord === inputValue && timePass) {
            setInputValue("");
            setRandomWord(words[Math.floor(Math.random() * words.length)]);
            setScope(scope + 1);
        }
    };

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question