Search code examples
react-nativereact-navigationswipereact-native-gesture-handler

Swipe to next screen (infinite)


I have a calendar section in my app where I want to be able to swipe right or left to go to the next or previous day.

I know that you can use createMaterialTopTabNavigator from React Navigation to do something similar, but I'm not sure if it would work in this specific scenario (where I would have an infinite number of screens and wouldn't be able to predefine them).

I also considered using react-native-gesture-handler's Swipeable component to navigate to the next/previous day screen on a left/right swipe but I'm not sure if this is the easiest/best way to approach this problem.


Solution

  • As you mentioned, you can use Tabs or Swipable. There are some other options like using <ifaram> or <WebView> and using a web URL instead of RN components but it's can be tricky and hard to manipulate.

    As a simple solution, you can use carousels instead of using multi screen defining navigation. It's very simple. assume that your screens are an image in an image gallery and you can swipe left/right.

    There are many libraries, one of the best ones is: react-native-snap-carousle

    Take a look at it's example:

    import Carousel from 'react-native-snap-carousel';
     
    export class MyCarousel extends Component {
     
        _renderItem = ({item, index}) => {
            return (
                <View style={styles.slide}>
                    <Text style={styles.title}>{ item.title }</Text>
                </View>
            );
        }
     
        render () {
            return (
                <Carousel
                  ref={(c) => { this._carousel = c; }}
                  data={this.state.entries}
                  renderItem={this._renderItem}
                  sliderWidth={sliderWidth}
                  itemWidth={itemWidth}
                />
            );
        }
    }
    

    So you can pass a screen/page as _renderItem to the Carousel