Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

javascript - React Native onBeforeSnapToItem与onPress结合(React Native onBeforeSnapToItem combined with onPress)

So this is a screen where you slide left to go forward and slide right to go back.(因此,这是一个屏幕,您在其中向左滑动可以前进,而向右滑动可以返回。)

there is also a button at the bottom, when clicked it skip all the slide and it goes to the main screen.(底部还有一个按钮,单击该按钮会跳过所有幻灯片,并转到主屏幕。) onPress={() => this.props.navigation.navigate('Mainscreen')} the issue here is that I would like to change this button so instead of skipping all the slide it just takes you to the next side, basically it will act the same as sliding left.(这里的问题是我想更改此按钮,因此与其跳过所有幻灯片,不如直接将它带到下一页,基本上它的作用与向左滑动相同。) and honestly I still didn't grasp how onBeforeSnapToItem works(老实说,我仍然不了解onBeforeSnapToItem的工作原理) render() { const width = Dimensions.get('window').width * 0.9 return ( <View style={loginStyles.containerExpand}> <View style={loginStyles.carouselOuter}> <ProgressDots current={this.state.currentSlide} total={SLIDE_COUNT} /> <Carousel data={this.slides} renderItem={this.renderItem} onBeforeSnapToItem={this.handleSlideChange} itemWidth={width} sliderWidth={width} /> </View> <View style={loginStyles.buttonContainer}> <TouchableHighlight style={loginStyles.button} onPress={() => this.props.navigation.navigate('Mainscreen')} underlayColor={Colors.buttonSecondaryBkgdActive} > <Text style={loginStyles.buttonText}>{this.buttonText}</Text> </TouchableHighlight> </View> </View> ) } } and this handleSlideChange function(和这个handleSlideChange函数) handleSlideChange = (currentSlide) => { this.setState({currentSlide}) }   ask by Alex Hernandez translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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> ... ) } }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...