0.1.0 • Published 5 years ago

react-native-swiper-parallax v0.1.0

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

Swiper parallax

react-native-swiper-parallax is a swiper component for React Native featuring paging and an optional parallax effect. Compatible with Android & iOS.

Getting started

npm install react-native-swiper-parallax --save

Usage

Content and delayed content are being passed as render props.

import Slider from 'react-native-swiper-parallax';

class MyComponent extends Component {
  renderContent = content => {
    return (
      <Content>
        <Text>content</Text>
      </Content>
    );
  };

  renderDelayedContent = content => {
    return (
      <DelayedContent>
        <Text>{content.title}</Text>
      </DelayedContent>
    );
  };

  render() {
    return (
      <Slider
        slides={slides}
        renderContent={this.renderContent}
        renderDelayedContent={this.renderDelayedContent}
        delayFactor={0.3}
        triggeringThreshold={0}>
        {(slideCount, currentIndex, nextIndex, requestSlideIndex) => (
          <Fragment>
            <PagingContainer>
              <Paging
                slideCount={slideCount}
                currentSlideIndex={currentIndex}
                currentControlColor={colors.mainOrange}
                defaultControlColor="rgba(46, 43, 43, 0.25)"
              />
            </PagingContainer>

            <BottomBar
              swipe={() => requestSlideIndex(currentIndex, currentIndex + 1)}
            />
          </Fragment>
        )}
      </Slider>
    );
  }
}

In depth

The Slider component is using React context api. It has a provider and every child which uses the HOC withSliderContext has access to the Slider store which is shown below.

const SliderContext = createContext({
  currentIndex: null,
  nextIndex: null,
  setCurrentIndex: () => {},
  setNextIndex: () => {},
  slideCount: null,
});

The Slider itself is inside the provider and has therefore access to an action which sets the next index before the animation is triggered. The Paging component while being connected to the Slider store is aware of any index change in real time. Slider has a shouldComponentUpdate method which prevents the component from re rendering when the nextIndex is set:

shouldComponentUpdate(nextProps: OwnProps) {
		return this.props.nextIndex === nextProps.nextIndex;
}

Props

PropsParamsReturnsDescription
slidescontent delayedContentnoneArray of slides with a content and a delayed content
renderContentcontentjsxRender all slides main content
renderDelayedContentcontentjsxRender all slides delayed content for parallax effect
delayFactornonenoneNumber to delay the sliding animation

OwnProps (withSliderContext HOC)

OwnPropsParamsReturnsDescription
currentIndexnonenoneSlider current index
nextIndexnonenoneSlider next index before animation is triggered
setCurrentIndexindexnoneSet current index
setNextIndexindexnoneSet next index
slideCountnonenoneNumber of slides