Skip to main content

ViewPager

ViewPager renders swipeable pages. Each child is rendered as a full-width page that the user can swipe between horizontally.

Import​

import { ViewPager } from '@ui-kitten/components';

Basic Usage​

import React, { useState } from 'react';
import { Layout, ViewPager, Text } from '@ui-kitten/components';

const ViewPagerExample = () => {
const [selectedIndex, setSelectedIndex] = useState(0);

return (
<ViewPager selectedIndex={selectedIndex} onSelect={setSelectedIndex}>
<Layout style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text category="h5">Page 1</Text>
</Layout>
<Layout style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text category="h5">Page 2</Text>
</Layout>
<Layout style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text category="h5">Page 3</Text>
</Layout>
</ViewPager>
);
};

Props​

PropertyTypeDefaultDescription
selectedIndexnumber0Index of the currently visible page.
onSelect(index: number) => void-Called when the visible page changes via swipe.
shouldLoadComponent(index: number) => boolean() => truePredicate to lazily load page content. Return false to skip rendering.
childrenReactNode[]-Page content elements.
swipeEnabledbooleantrueWhether pages can be changed by swiping.
animationDurationnumberundefinedDuration of the page transition animation, in milliseconds.
onOffsetChange(offset: number) => voidundefinedCalled while the pager scrolls, with the current horizontal offset in pixels (offset / pageWidth is the page position).
onLayout(event: LayoutChangeEvent) => voidundefinedCalled with the layout of the content strip, pages x page width wide.

ViewPager also accepts all standard React Native View props.

PageIndicator​

PageIndicator renders one dot per page and highlights the selected one. Feed it selectedIndex from the pager; for dots that follow the swipe, pass progress computed from onOffsetChange divided by the pager width. Pressing a dot calls onSelect.

const [selectedIndex, setSelectedIndex] = useState(0);
const [progress, setProgress] = useState(0);
const [pageWidth, setPageWidth] = useState(0);

<ViewPager
selectedIndex={selectedIndex}
onSelect={setSelectedIndex}
// The pager lays out its content strip, pages x page width wide.
onLayout={event => setPageWidth(event.nativeEvent.layout.width / pages.length)}
onOffsetChange={offset => pageWidth > 0 && setProgress(offset / pageWidth)}
>
{pages}
</ViewPager>
<PageIndicator
pageCount={pages.length}
selectedIndex={selectedIndex}
progress={progress}
onSelect={setSelectedIndex}
dotAccessibilityLabel={(index, count) => `Page ${index + 1} of ${count}`}
/>
PropertyTypeDefaultDescription
pageCountnumber-Number of pages, one dot each.
selectedIndexnumber0Index of the selected page.
progressnumberundefinedFractional page position while the pager scrolls (offset / pageWidth); the dots grow and shrink between pages.
onSelect(index: number) => voidundefinedCalled with the index of a pressed dot.
status'basic' | 'primary' | 'success' | 'info' | 'warning' | 'danger' | 'control''primary'Colour of the selected dot.
dotStyleViewStyleundefinedStyle of every dot, merged after the mapping.
dotAccessibilityLabel(index, pageCount) => stringundefinedAccessible name of a dot.
appearance'default''default'Appearance defined in the Eva mapping.

The dot size, spacing, colours and the selected dot width come from the PageIndicator mapping (dotWidth, dotHeight, dotBorderRadius, dotMarginHorizontal, dotBackgroundColor, selectedDotWidth, selectedDotBackgroundColor per status).

Storybook​

Live examples coming soon via Storybook.