React native 我正在使用react native swiper flatlist,但我不知道如何在按下按钮时使用scrollToIndex
我使用的是react native swiper flatlist,我想通过点击按钮向前滚动到某个索引,但不知道如何操作。虽然我是一个非常初学者,以反应本地开发 我已经搜索了很多,但没有任何帮助,我得到了一个错误:undefined不是对象this2.swiper.scrollToIndex'React native 我正在使用react native swiper flatlist,但我不知道如何在按下按钮时使用scrollToIndex,react-native,swiper,React Native,Swiper,我使用的是react native swiper flatlist,我想通过点击按钮向前滚动到某个索引,但不知道如何操作。虽然我是一个非常初学者,以反应本地开发 我已经搜索了很多,但没有任何帮助,我得到了一个错误:undefined不是对象this2.swiper.scrollToIndex' render() { return ( <View style={styles.container}> <SwiperFlatList ref={swiper =
render() {
return (
<View style={styles.container}>
<SwiperFlatList
ref={swiper => {
this.swiper = swiper;
}}
data={[
{ key: "a" },
{ key: "b" },
{ key: "c" },
{ key: "d" },
{ key: "e" }
]}
index={0}
renderItem={({ item, index }) => (
<View>
<Image
style={styles.child}
source={require("../../assets/advertisementtwo.png")}
/>
<Image
style={styles.child}
source={require("../../assets/advertisementtwo.png")}
/>
<Image
style={styles.child}
source={require("../../assets/advertisementtwo.png")}
/>
<Image
style={styles.child}
source={require("../../assets/advertisementtwo.png")}
/>
<Button
title={"Next"}
onPress={this.swiper.scrollToIndex(1, true)}
style={{ backgroundColor: "white" }}
/>
</View>
)}
/>
</View>
);
render(){
返回(
{
this.swiper=swiper;
}}
资料={[
{key:“a”},
{键:“b”},
{键:“c”},
{键:“d”},
{键:“e”}
]}
索引={0}
renderItem={({item,index})=>(
)}
/>
);
}
应该在按钮点击时滑动吗
尝试将
ref='swiper'
添加为swiperplatlist
prop
示例
<SwiperFlatList ref='swiper'/>
this.refs.swiper.\u scrollToIndex(2)
我已经解决了问题“onPress={this.swiper.scrollToIndex(3)}”应该替换为onPress={()=>{this.refs.swiper.\u scrollToIndex(3)}谢谢您需要设置this.refs.swiper.scrollToIndex(2)
,并声明引用作为示例