In useEffect-hook I set interval, which is running function "calculateCircle". There I do some logic, including setting state(with useState-Hook). Variables from hooks are updated, I render and see them on page, but this function keeps consolloging old values.
I changed my component to the class-based component (without hooks) and everything is working now. But I wonder which is the problem using hooks.
const Features = () => {
const block1 = React.createRef();
const shadowText = React.createRef();
const [ mouseIn, setMouseIn ] = useState(false);
const [ xCircle, setXCircle] = useState(-50);
const calculateCircle = () => {
console.log('mouseIn :', mouseIn); //never changes, but in page - yes
if (!mouseIn) { //never skips this loop
console.log('begin', xCircle);//always the same
let r = 50;
let yCircle = Math.sqrt(r*r - xCircle*xCircle);
if (shadowText.current) draw(xCircle, yCircle);
setXCircle(prev => {
console.log('xCircle:', prev);
return prev > 50 ? -50 : prev + 1
});
console.log('end', xCircle, yCircle);
}
} //on page I see that xCircle changes correctly
useEffect(() => {
const cycle = setInterval(() => calculateCircle(), 1000);
return () => {
clearInterval(cycle);
}
}, []);
//without hooks it works;