admin管理员组文章数量:1314411
I'm working with React Native maps with an animated FlatList on top and when I press on a map marker, I want the list to scroll to that particular index. But when I try to do that I get this error _this.flatListRef.scrollToIndex is not a function. '_this.flatListRef.scrollToIndex' is undefined
The basics of my code look like this...
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
export default Map extends ponent {
scrollToCard = index => {
this.flatListRef.scrollToIndex({index})
}
render() {
const { allProperties } = this.props;
<MapView
ref={map => this.map = map}
style={styles.map}
initialRegion={{
latitude: 35.2271,
longitude: -80.8431,
latitudeDelta: 0.0922,
longitudeDelta: 0.421
}}
>
{allProperties && allProperties.map((property, index) => (
<Marker
coordinate={property.coordinates}
title={property.propertyName}
pinColor={theme.COLOR_GREEN}
key={property.id}
onPress={() => this.scrollToCard(index)}
/>
))}
</MapView>
{allProperties &&
<AnimatedFlatList
ref={ref => {this.flatListRef = ref }}
data={allProperties}
getItemLayout={(data, index) => (
{length: CARD_WITH_MARGIN, offset: CARD_WITH_MARGIN * index, index}
)}
initialNumToRender={2}
horizontal
scrollEventThrottle={1}
showsHorizontalScrollIndicator={false}
snapToInterval={CARD_WITH_MARGIN}
onScroll={Animated.event(
[
{
nativeEvent: {
contentOffset: {
x: this.animation
},
},
},
],
{ useNativeDriver: true }
)}
style={styles.scrollView}
contentContainerStyle={styles.scrollContainer}
renderItem={this._renderItem}
keyExtractor={item => item.id}
/>
}
}
}
Mainly it seems like my ref of flatListRef is undefined for some reason. At least that's my theory but I can't tell why.
Can anyone spot what I'm doing wrong here?
If it helps, allProperties
is data ing back from a graphql query.
I'm working with React Native maps with an animated FlatList on top and when I press on a map marker, I want the list to scroll to that particular index. But when I try to do that I get this error _this.flatListRef.scrollToIndex is not a function. '_this.flatListRef.scrollToIndex' is undefined
The basics of my code look like this...
const AnimatedFlatList = Animated.createAnimatedComponent(FlatList);
export default Map extends ponent {
scrollToCard = index => {
this.flatListRef.scrollToIndex({index})
}
render() {
const { allProperties } = this.props;
<MapView
ref={map => this.map = map}
style={styles.map}
initialRegion={{
latitude: 35.2271,
longitude: -80.8431,
latitudeDelta: 0.0922,
longitudeDelta: 0.421
}}
>
{allProperties && allProperties.map((property, index) => (
<Marker
coordinate={property.coordinates}
title={property.propertyName}
pinColor={theme.COLOR_GREEN}
key={property.id}
onPress={() => this.scrollToCard(index)}
/>
))}
</MapView>
{allProperties &&
<AnimatedFlatList
ref={ref => {this.flatListRef = ref }}
data={allProperties}
getItemLayout={(data, index) => (
{length: CARD_WITH_MARGIN, offset: CARD_WITH_MARGIN * index, index}
)}
initialNumToRender={2}
horizontal
scrollEventThrottle={1}
showsHorizontalScrollIndicator={false}
snapToInterval={CARD_WITH_MARGIN}
onScroll={Animated.event(
[
{
nativeEvent: {
contentOffset: {
x: this.animation
},
},
},
],
{ useNativeDriver: true }
)}
style={styles.scrollView}
contentContainerStyle={styles.scrollContainer}
renderItem={this._renderItem}
keyExtractor={item => item.id}
/>
}
}
}
Mainly it seems like my ref of flatListRef is undefined for some reason. At least that's my theory but I can't tell why.
Can anyone spot what I'm doing wrong here?
If it helps, allProperties
is data ing back from a graphql query.
1 Answer
Reset to default 9In case anyone finds this post and is having the same issue (or for me later on when I forget), I've found the answer here...
It looks like, in order to use a Ref with private variable like the animated one I created above the ponent, you need to use getNode()
when you call it.
For instance, in my scrollToCard()
function it should have done the following...
this.flatListRef.getNode().scrollToIndex({index})
本文标签: javascriptReact Native Maps FlatList scrollToIndex() not a functionundefinedStack Overflow
版权声明:本文标题:javascript - React Native Maps FlatList scrollToIndex() not a functionundefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741893885a2403461.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论