0

I have six images,i want change this images with two button, back button and next button.If i using two images no problem but i using six images have problem.

image1 ss2 ss3

  • 2
    Welcome to [so]! Questions along the lines of "I want to do X; can someone tell me how to write my code?" are considered off-topic on Stack Overflow. Please see [help/on-topic] for more details. Note that if your question is, "I want to do X and I tried approach Y but it didn't work; can anyone help me figure out what I did wrong?" then that question is on-topic on Stack Overflow, and you'll find many people willing to help you with your problem. – lucascaro Mar 04 '19 at 19:06

1 Answers1

0

So you have to keep the data in a array and do a setState of the index on button click to the next one. Suppose current index is 0, when you click on next, increment the index to 1 and do a setState. For example,

import React, { Component } from 'react'
// import PropTypes from 'prop-types';
import { View, TouchableOpacity, Image, Text } from 'react-native';

export default class App extends Component {

    constructor(props) {
        super(props);
        this.state = {
            images: [],
            selectedIndex: 0
        }
    }

    _ToggleNext = () => {
        if(this.state.selectedIndex == this.state.images.length - 1)
            return;

        this.setState(prevState => ({
            selectedIndex: prevState.selectedIndex + 1
        }))
    }

    _TogglePrev = () => {
        if(this.state.selectedIndex == 0)
         return;

        this.setState(prevState => ({
            selectedIndex: prevState.selectedIndex - 1
        }))
    }

    render() {
        const {selectedIndex, images} = this.state;
        return (
             <View>
                  <Image
                    source={{ uri: images[selectedIndex]}}
                    style={styles.image}
                  />
                  <TouchableOpacity onPress={this._ToggleNext}>
                    <Text>Next</Text>
                  </TouchableOpacity>
                  <TouchableOpacity onPress={this._TogglePrev}>
                    <Text>Pr</Text>
                  </TouchableOpacity>
             </View>
        )
    }
}

EDIT: Link - https://snack.expo.io/rysnt5iUV

Subhendu Kundu
  • 3,618
  • 6
  • 26
  • 57