chilijung / react-native-carousel-view

react-native carousel, support in both Android and iOS
73 stars 44 forks source link

Next and previous buttons on carousel #11

Open bnap00 opened 6 years ago

bnap00 commented 6 years ago

It would really come in handy if we can implement next and previous buttons on the carousel

jitenderbadoni commented 6 years ago

Make below changes in node_modules/react-native-carousel-view/src/carousel.js

     componentDidMount() {
        this._resetPager();
        this.props.onRef(this);  // add this line
     }

Add this method :

     componentWillMount(){
         this.props.onRef(undefined);
     }

Change below changes in your component:

Add this method:

          _onNext = () => {
               this.child._animateNextPage() // do stuff
             }

Change this with below code:

          <Carousel
                    height={600}
                    indicatorAtBottom={true}
                    indicatorSize={20}
                    animate={false}
                    onRef={ref => (this.child = ref)}
                    >
              <View style={styles.contentContainer}>
                      <Text>Page 1</Text>
                    </View>
              <View style={styles.contentContainer}>
                      <Text>Page 2</Text>
                    </View>
              <View style={styles.contentContainer}>
                      <Text>Page 3</Text>
                    </View>
       </Carousel>
      <Button onPress={this._onNext}>
                        <Text style={styles.nextText}>Next</Text>
          </Button>

It works with me.!

mehrdadfeller commented 6 years ago

can you please calrify where to add this part:

      _onNext = () => {
           this.child._animateNextPage() // do stuff
         }

I am getting the following error:

E/ReactNativeJS( 2848): TypeError: undefined is not a function (evaluating 'this.props.onRef(this)')

mehrdadfeller commented 6 years ago

okay my bad I did not see this line

onRef={ref => (this.child = ref)}

when creating the carousel.

jitenderbadoni commented 6 years ago

work well now.?

Spoutnik97 commented 6 years ago

@jitenderbadoni you should do a pull request ! :)

vince83110 commented 5 years ago

Is there any other way ? without editing the plugin code ?

jitenderbadoni commented 5 years ago

@vince83110 didn't see any option yet.!

vince83110 commented 5 years ago

Ok, in last version, this works :

_onNext = () => {
    this._carousel._animateNextPage(); // do stuff
};

And :

<Carousel
    // ... config
    ref={ (c) => this._carousel = c }
>
jitenderbadoni commented 5 years ago

@chilijung please review the above code!