The onBeforeSnapToItem
property is just a placeholder for a callback function which is called by the component.(onBeforeSnapToItem
属性只是该组件调用的回调函数的占位符。)
What you need is to get a reference to the carousel component and call the snapToNext
method of the component in your onPress
function.(您需要获得对轮播组件的引用,并在onPress
函数中调用该组件的snapToNext
方法。) See doc of react-native-snap-carousel(请参阅react-native-snap-carousel的文档)
Getting a reference can be done with React.createRef()
:(可以使用React.createRef()
获得引用:)
class Example extends Component {
carousel = React.createRef()
goToNextSlide = () => {
this.carousel.current.snapToNext()
}
...
render() {
return (
...
<Carousel
ref={this.carousel}
data={this.slides}
renderItem={this.renderItem}
onBeforeSnapToItem={this.handleSlideChange}
itemWidth={width}
sliderWidth={width}
/>
...
<TouchableHighlight
style={loginStyles.button}
onPress={this.goToNextSlide}
underlayColor={Colors.buttonSecondaryBkgdActive}
>
<Text style={loginStyles.buttonText}>{this.buttonText}</Text>
</TouchableHighlight>
...
)
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…