react-native-carousel-view icon indicating copy to clipboard operation
react-native-carousel-view copied to clipboard

Next and previous buttons on carousel

Open bnap00 opened this issue 7 years ago • 9 comments

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

bnap00 avatar Jan 31 '18 09:01 bnap00

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.!

jitenderbadoni avatar Jul 08 '18 07:07 jitenderbadoni

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 avatar Aug 23 '18 22:08 mehrdadfeller

okay my bad I did not see this line

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

when creating the carousel.

mehrdadfeller avatar Aug 23 '18 23:08 mehrdadfeller

work well now.?

jitenderbadoni avatar Aug 24 '18 06:08 jitenderbadoni

@jitenderbadoni you should do a pull request ! :)

Spoutnik97 avatar Sep 07 '18 13:09 Spoutnik97

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

vince83110 avatar Nov 19 '18 17:11 vince83110

@vince83110 didn't see any option yet.!

jitenderbadoni avatar Nov 19 '18 17:11 jitenderbadoni

Ok, in last version, this works :

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

And :

<Carousel
	// ... config
	ref={ (c) => this._carousel = c }
>

vince83110 avatar Nov 20 '18 08:11 vince83110

@chilijung please review the above code!

jitenderbadoni avatar Mar 04 '19 13:03 jitenderbadoni