K
K
Konstantin Chuykov2016-08-08 00:35:55
JavaScript
Konstantin Chuykov, 2016-08-08 00:35:55

How to use SwitchIOS in a loop in React Native?

Hello! I am making a mobile application using React Native 0.30.
There is a Report component that receives data through props.

export default class Report extends Component {
    constructor(props) {
      super(props);
      
      this.state = {
        questions : [],
        progress : 0,
        activeSlide : 0
      }
    }

    setActiveSlide(i, inc) {
      let max = this.props.check.questions.length;
      let curr = inc ? i+1 : i-1;
      let progress = curr / max;
      this.setState({
        activeSlide : curr,
        progress : progress
      })
    }

Each slide is rendered with a function:
renderSlide(slide, i) {
      if(this.state.activeSlide == i)
      return(
        <View>
          <Text>{slide.position} / {this.props.check.questions.length}. {slide.title}</Text>
          <SwitchIOS
            onValueChange={(value) => this.setCheck(value, i)}
            value={/*anything*/}
          />
          <View>
            <TouchableHighlight onPress={() => this.setActiveSlide(i)}>
                <Text>Назад</Text>
            </TouchableHighlight>
            <TouchableHighlight onPress={() => this.setActiveSlide(i, true)}>
                <Text>Далее</Text>
            </TouchableHighlight>
          </View>
        </View>
      )
    }

And now I don’t quite understand what to do with SwitchIOS, whether I should write all the marks in a separate state.questions, or should I change the existing data array. If done by adding to state.questions, like this:
setCheck(val, i) {
      this.state.questions.push({index : 1, check : val});
    }

So how do I change the state of the switch? What value to specify? If there are other ways please share.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question