我需要一个视图来动画的地方时,一些props 的价值发生变化,使用 react native 重生
例如,我只能在使用onPress函数更改useSharedValue值的用户点击上找到动画.但我似乎找不到如何在props 变化时设置动画.因此,目前我使用的是useEffect.
export const SomeComponent = ({
top,
left
}) => {
const animation = useSharedValue({
top,
left
});
useEffect(() => {
animation.value = { top, left };
}, [top, left]);
const animationStyle = useAnimatedStyle(() => {
return {
top: withTiming(animation.value.top, {
duration: 1000
}),
left: withTiming(animation.value.left, {
duration: 1000
})
};
});
}
在上面的代码中,props 可以改变值,我应该像现在一样使用useEffect触发动画吗?或者有没有更好的方法用复活的.