我尝试使用Hooks为自己制作一个很好的动画示例,但是我偶然发现了一个问题,即我的函数没有状态变量的更新版本,而继续使用第一个版本。
在下面的代码段中,我有一个示例,当您单击一个条时,它应该以正方形形式移动。首先向东,然后向南,然后向西,然后向北,然后再向东,等等。然而,它永远不会向南,因为即使其方向已从north
更改为east
(如栏上的文字所示)或再次单击它),这些功能仍然认为方向是北。
const Example = (props) => {
const [ direction, setDirection ] = React.useState('North');
console.log("Rendering Example: ", direction);
const isAnimating = React.useRef(false)
const blockRef = React.useRef(null);
// Animate on click.
const onClick = () => {
if (!isAnimating.current) {
decideDirection();
isAnimating.current = true
} else {
console.log("Already animating. Going: ", direction);
}
};
const decideDirection = () => {
console.log("Current direction: ", direction);
if (direction === 'North') {
moveEast();
} else if (direction === 'East') {
moveSouth();
} else if (direction === 'South') {
moveWest();
} else if (direction === 'West') {
moveNorth();
}
};
const move = (toX, toY, duration, onComplete) => {
Velocity(blockRef.current, {
translateX: toX,
translateY: toY,
complete: () => {
onComplete();
}
},
{
duration: duration
});
}
const moveNorth = () => {
setDirection('North');
console.log('Moving N: ', direction);
move(0, 0, 500, () => {
decideDirection();
})
}
const moveEast = () => {
setDirection('East');
console.log('Moving E: ', direction);
move(500, 0, 2500, () => {
decideDirection();
})
};
const moveSouth = () => {
setDirection('South');
console.log('Moving S: ', direction);
move(500, 18, 500, () => {
decideDirection();
})
}
const moveWest = () => {
setDirection('West');
console.log('Moving W: ', direction);
move(0, 18, 2500, () => {
decideDirection();
})
}
return(
<div>
<div id='block' onClick={onClick} ref={blockRef} style={{ width: '100px', height: '18px', backgroundColor: 'red', textAlign: 'center'}}>{direction}</div>
</div>
);
};
ReactDOM.render(<div><Example/></div>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.2/velocity.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id='root' style='width: 100%; height: 100%'>
</div>
我觉得很奇怪,因为这些功能都没有被记忆,因此它们应该重新创建每个渲染并因此具有新的价值。即使我确实添加了useCallback
之类的内容并为每个函数提供了direction
,它仍然无法正常工作。函数为什么不知道状态var的更新版本?
答案 0 :(得分:3)
问题是您的move
函数在decideDirection
的初始版本中已关闭。您的div已重新渲染,但动画会继续引用该函数的初始版本。解决此问题的一种方法是使用ref指向decideDirection
函数(我已在下面的代码段中显示了此方法)。
这似乎有点脆弱,因为重新渲染的时间与动画的时间使得很难推断它是否可靠。一种更可靠的方法是通过状态更改更明确地链接动作,这样,您不必设置在每个动画完成时直接调用decideDirection
的状态,而是触发一个效果来开始下一个动画。
const Example = (props) => {
const [ direction, setDirection ] = React.useState('North');
console.log("Rendering Example: ", direction);
const isAnimating = React.useRef(false)
const blockRef = React.useRef(null);
const decideDirection = () => {
console.log("Current direction: ", direction);
if (direction === 'North') {
moveEast();
} else if (direction === 'East') {
moveSouth();
} else if (direction === 'South') {
moveWest();
} else if (direction === 'West') {
moveNorth();
}
};
const decideDirectionRef = React.useRef(decideDirection);
React.useEffect(()=>{
decideDirectionRef.current = decideDirection;
});
// Animate on click.
const onClick = () => {
if (!isAnimating.current) {
decideDirectionRef.current();
isAnimating.current = true
} else {
console.log("Already animating. Going: ", direction);
}
};
const move = (toX, toY, duration, onComplete) => {
Velocity(blockRef.current, {
translateX: toX,
translateY: toY,
complete: () => {
onComplete();
}
},
{
duration: duration
});
}
const moveNorth = () => {
setDirection('North');
console.log('Moving N: ', direction);
move(0, 0, 500, () => {
decideDirectionRef.current();
})
}
const moveEast = () => {
setDirection('East');
console.log('Moving E: ', direction);
move(500, 0, 2500, () => {
decideDirectionRef.current();
})
};
const moveSouth = () => {
setDirection('South');
console.log('Moving S: ', direction);
move(500, 18, 500, () => {
decideDirectionRef.current();
})
}
const moveWest = () => {
setDirection('West');
console.log('Moving W: ', direction);
move(0, 18, 2500, () => {
decideDirectionRef.current();
})
}
return(
<div>
<div id='block' onClick={onClick} ref={blockRef} style={{ width: '100px', height: '18px', backgroundColor: 'red', textAlign: 'center'}}>{direction}</div>
</div>
);
};
ReactDOM.render(<div><Example/></div>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.2/velocity.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id='root' style='width: 100%; height: 100%'>
</div>
答案 1 :(得分:0)
你的价值被捕获,看看my another answer