我有一个小动画的问题。我尝试在我的应用上为错误做一个Flash Bar Info。为此,我创建了一个新组件,这个组件可以放置在一个特定的视图中,当错误从我的商店触发时,我的组件的状态由componentWillReceiveProps
更改,并设置为可见,如果有错误消息。React本机取消动画延迟当组件将卸载
因此,如果没有一个错误消息我渲染函数返回值为false,但如果我跑我的错误信息渲染这个动画代码功能:
// Ease in ease out anitmation
Animated.sequence([
Animated.timing(this.state.translateY, {
toValue: 40,
easing: Easing.bounce, // Like a ball
duration: 450,
}),
Animated.delay(3000),
Animated.timing(this.state.translateY, {
toValue: 0,
duration: 300,
}),
]).start(() => {
this.props.clearMessage();
this.setState({ visible: false }); // <-- Problem is here
});
如果我留在我的观点在动画延迟期间,这是完美的工作,但如果我在前面的视图中将消息设置为可见时,则在未装入组件时调用函数setState
。 所以我得到这个华林:
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the undefined component.
这是一种正常的行为,因为该组件没有被安装在这一点上。 所以,我试图找到一种方法来取消我的动画序列,当组件将卸载。
在我的研究,我发现通过使用clearTimeout
setTimeout()
功能时,该组件将卸载暂时的方式,但我无法找到如何做到这一点与Animated.delay
功能在Animated.sequence
,这可能吗?
提前感谢您的答案!
TLDR;
组件卸载时可以取消动画延迟吗?
该文档提'stopAnimation'方法。你有没有试过看它?它是[这里](https://facebook.github.io/react-native/docs/animated.html#stopanimation)。 –
感谢Hamaide,但是,我也尝试过,但是'Animated.sequence'没有找到函数'stopAnimation'。 –