我有一个自定义模式,它在componentDidMount
时滑入:
componentDidMount() {
Animated.timing(this._animatedValue.y, {
duration: 200,
toValue: 0
}).start()
}
好吧,这很简单.然而,我也喜欢在组件卸载时滑出模式.对我来说,componentWillUnmount()
应该是正确的,因为它是一种优雅的陈述方式:
componentWillUnmount() {
Animated.timing(this._animatedValue.y, {
duration: 200,
toValue: deviceHeight
}).start()
}
但这当然有not种效果,因为React不会等到我完成动画.
现在我用一个自定义函数来解决这个问题:
closeModal() {
Animated.timing(this._animatedValue.y, {
duration: C.filterModalDuration,
toValue: deviceHeight
}).start()
InteractionManager.runAfterInteractions(() => {
this.props.UnmountThisComponent()
})
}
这当然不是那么优雅,但它很管用.然而,如果我需要从组件树中较低的组件调用此函数,那么痛苦就开始了,也就是说,我需要通过onUnmount={()=> closeModal()}
手动传递此函数,然后通过onUnmount={this.props.onUnmount}
反复传递...
然后我想我可以用redux&;redux connect.我想从子组件触发redux状态更改,然后通过componentWillRecieveProps()
调用函数,如下所示:
componentWillReceiveProps (nextProps) {
if (nextProps.closeFilter === true) {
this.closeModal()
}
}
然而,这让人感觉非常刻薄和迫切.
Is there any way to solve this problem in an elegant / declarative way?