[英]In React Native - is it possible to add a listener on an Animated Value received through props?
我在我的React Native應用程序中有一個ScrollView
,我有幾件需要根據當前滾動位置完成的事情 - 其中一些是ScrollView
本身,另一些則影響其他組件中的動畫。
理想情況下,我想通過他們的道具將滾動位置的Animated.Value
傳遞給其他組件,讓他們在值上添加一個監聽器並做他們的事情。 但據我所知,需要在componentWillMount
添加監聽器,我還沒有訪問道具。 因此,我不得不完成所有監聽top組件中值的工作,該組件同時包含ScrollView
和依賴它的其他組件。
我理想的代碼應該是這樣的:
<MyTopComponent>
<ScrollView onScroll={Animated.event([{nativeEvent: {contentOffset: {y: this.state.scrollPositionY}}}])}
<OtherComponentA scrollPositionY={this.state.scrollPositionY} />
<OtherComponentB />
</MyTopComponent>
然后在OtherComponentA
:
this.props.scrollPositionY.addListener(this._listener.bind(this));
但是我不能在OtherComponentA.componentWillMount
這樣做,因為道具還沒有。 所以我必須在MyTopComponent.componentWillMount
這樣做。
我錯過了什么嗎?
是否有另一種方法可以在通過道具收到的Animated.Value
上添加偵聽器?
改為在componentDidMount()
添加偵聽器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.