“setInterval”中的函数不会从钩子接收更新的变量

IT技术 reactjs setinterval react-hooks
2021-05-01 04:01:32

在 useEffect-hook 中,我设置了运行函数“calculateCircle”的间隔。在那里我做了一些逻辑,包括设置状态(使用 useState-Hook)。来自钩子的变量被更新,我渲染并在页面上看到它们,但这个函数不断地合并旧值。

我将我的组件更改为基于类的组件(没有钩子),现在一切正常。但我想知道使用钩子的问题是什么。

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);
    }
}, []);

//没有钩子就可以工作;

1个回答

由于calculateCircle 实例最初仅从useEffect 钩子内部引用,因此在创建此函数时,它从闭包中获取xCircle 和mouseIn 值,对于setInterval 是在初始调用时。

您需要将第二个参数传递给 useEffect,以便在 xCircle 或 mouseIn 更改时再次创建此方法

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);
    }
}, [mouseIn, xCircle]);