1.0.4 • Published 6 years ago
another-react-native-lazy-swiper v1.0.4
Another React Native Lazy Swiper
Another swiper for React Native with lazy-loaded contents.
Installation
npm install another-react-native-lazy-swiper --save
Usage
- Define
onSwipeEndprops: in this function, set nextIndex of component's state to currentIndex prop then pass the rerender callback function to setState method as second argument. - Define
renderItemprops: use this function to render each content for the swiper - Optional Define
widthfor child views. Default width is screen width. - To swipe next manually:
lazySwiper.swipeNext() - To swipe back manually:
lazySwiper.swipeBack() - To go to specific item (no animation supported): set
currentIndexof component's state to specific index.
Example:
import { Text, View, Dimensions, StyleSheet } from 'react-native'
import LazySwiper from 'another-react-native-lazy-swiper'
import React, { useRef } from 'react'
import { SafeAreaView } from 'react-navigation'
const styles = StyleSheet.create({
container: {
flex: 1,
},
})
const screenwidth = Dimensions.get('window').width
const MySwiper = ({ data }) => {
const [currentIndex, setCurrentIndex] = useState(index)
const _lazySwiper = useRef()
const prev = () => {
_lazySwiper.swipeBack()
}
const goTo = index => {
setCurrentIndex(index)
}
const next = () => {
_lazySwiper.swipeNext()
}
const onSwipeEnd = (nextIndex, rerender) => {
rerender()
setCurrentIndex(nextIndex)
}
const renderItem = item => {
return (
<SafeAreaView style={styles.container}>
<Text>{item}</Text>
</SafeAreaView>
)
}
return (
<LazySwiper
currentIndex={currentIndex}
data={data}
width={screenwidth}
renderItem={renderItem}
ref={_lazySwiper}
onSwipeEnd={onSwipeEnd}
/>
)
}
export default MySwiperProps
currentIndex: (number, isRequired) to set specific displayed content of the swiper.data: (array, isRequired) list of items to show swiper contents.renderItem: (function(item, index), isRequired) to render each swiper content. Parameters:item: item ofdataarrayindex: index of the item indataarray
onSwipeEnd: (function(nextIndex, rerender), isRequired) to set to new currentIndex and rerender the swiper. Define this will help the swiper render new content and swipe out unnessessary content. Parameters:nextIndex: (number) set this to currentIndex of the state of the component which render the swiper.rerender: (function()) pass this tosetStatemethod from above as second argument (sorry for this mysterious callback function but I found no other way).
LICENSE
MIT