我试图滚动一个sectionList到一个特定的部分时,一个按钮被点击。我每次都得到这个错误:
Uncaught TypeError: scrollRef。scrollToLocation不是一个函数
我错过了什么?
var scrollRef = useRef()
const onScroll = () => {
scrollRef.current.scrollToLocation({
sectionIndex: 1,
viewPosition: 0,
itemIndex: 1,
})
}
<SectionList
style={styles.sectionList}
ListHeaderComponent={<ChefHighlight chef={props.chef}/>}
keyExtractor={(item, index) => item.name}
sections={ data }
renderSectionHeader={({ section }) => {
return (
<View style={styles.headerView}>
<Text style={[globalStyle.textPrimary, styles.sectionTitle]}>{section.title}</Text>
<View style={{paddingLeft: 20, alignItems:'center', flexDirection: 'row'}}>
<MaterialIcons name="date-range" size={18} color={colors.textTertiary} />
<Text style={{alignSelf: 'center', textAlign: 'center', marginBottom: 6, marginLeft: 8, color: colors.textTertiary, fontSize: 14, marginTop: 8, }}>Delivers on {section.title}'s</Text>
</View>
</View>
);
}}
renderItem={({ item }) => {
return <ComboCell item={item} onOpen={onOpen}/>
}}
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { y: scrollY }}}],
// { useNativeDriver: true }
)}
ref={scrollRef}
ListFooterComponent={<View style={{width: '100%', height: 100}}/>}
/>
如果使用scrollToLocation
,则必须添加getItemLayout