Skip to main content

DynamicItemScrollSwiper

auto scrolling using scroll view

Overview

dynamic-swiper-example

Platform

iOSAndroidWeb
OOO

Basic usage

import React, { useCallback, useState } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';
import { DynamicItemScrollSwiper } from 'react-native-awesome-swiper';

const App = () => {
const [activeIndex, setActiveIndex] = useState(0);

const items = ['Hi', 'Jerry', 'FrontEnd', 'Developer', 'Apple', 'Coding'];

return (
<DynamicItemScrollSwiper
data={items}
horizontal
contentContainerStyle={{ paddingHorizontal: 20 }}
activeIndex={activeIndex}
gap={10}
viewOffset={40}
renderItem={(item, index) => (
<Pressable
onPress={() => setActiveIndex(index)}
style={[
styles.item,
activeIndex === index
? { backgroundColor: '#A4D0A4' }
: { backgroundColor: '#eeeeee' },
]}>
<Text>{item}</Text>
</Pressable>
)}
/>
);
};

const styles = StyleSheet.create({
item: {
paddingHorizontal: 20,
paddingVertical: 8,
borderRadius: 10,
alignItems: 'center',
justifyContent: 'center',
},
});

Props

extends all ScrollViewProps

activeIndex

currently active index

TypeDefaultRequired
numberundefinedYES

data

items to render

TypeDefaultRequired
T[]undefinedYES

renderItem

render item function

TypeDefaultRequired
(item: T, index: number) => React.ReactElementundefinedYES

keyExtractor

uses index default

TypeDefaultRequired
(item: T, index: number) => stringundefinedNO

gap

gap between items

TypeDefaultRequired
number0NO

viewOffset

view offset when scrolling to activeIndex item

TypeDefaultRequired
number20NO

shouldScrollOnMount

flag to decide scroll on mount

If initail activeIndex is not 0 and shouldScrollOnMount is false, contents may not be visible for few milliseconds while layout calculation is on-going to decide initial scroll offset

TypeDefaultRequired
booleanfalseNO