In render()
of my RN component class I call functional component A which loads a Carousel. This Carousel in A then calls functional component B in its renderItem
prop. B contains a FlatList w ref defined as <FlatList ref={ref => (this.list = ref)} />
.
So A is successfully accessing this.list.scrollToIndex()
in an onTouch call defined in A. What I need to do is call this.list.scrollToOffset()
in B where the FlatList and the ref are defined - but when I do that I get an error TypeError: TypeError: null is not an object (evaluating '_this.list.scrollToOffset')
I've tried using React.createRef() at the class level but that is not working either.
Here is the code:
//component B
renderList = () => {
if (scrollPosition>0) {
//this call to list fails
this.list.scrollToOffset({
offset: scrollPosition
})
}
return (
<FlatList
ref={ref => (this.list = ref)}
...
/>
)
}
//component A
renderCarousel = () => {
return (
<View style={{ flex: 1 }}>
<View>
<TouchableOpacity
onPress={() => {
this.list.scrollToIndex({ index: 0 }) //this call to list works
}}
>
</TouchableOpacity>
</View>
{
showList && (
<Carousel
renderItem={this.renderList}
/>
)
}
</View>
)
}
I'm guessing I'm in that weird and dreaded js this
zone. Any ideas?? TIA!