react-native - How to use FlatList to create multiple switches?

拜拜、爱过 提交于 2019-12-24 00:09:32

问题


I want to have a list of multiple switches to essentially create a list of activities for the user to then "select/check" if they are interested in one or more.

My plan was to use a switch as the renderItem of a Flatlist, but I am having trouble with two things.

1) I can't get the switch to stay toggled on when it is in the Flatlist. I had it working at one point but messed it up since.

2) When it was working, all the switches would toggle together.

Any help would be much appreciated!

import React, { Component } from 'react';
import { FlatList, StyleSheet, Text, View, Switch } from 'react-native';

class InterestsList extends Component {
  constructor() {
    listKeys = [
      {key: 'Basketball'},
      {key: 'Football'},
      {key: 'Baseball'},
      {key: 'Soccer'},
      {key: 'Running'},
      {key: 'Cross Training'},
      {key: 'Gym Workout'},
      {key: 'Swimming'},
    ];

    super();
    this.state = {
       switchValue: false
    }
  }

  toggleSwitch = (value) => {
    this.setState({switchValue: value})
    console.log('Switch is: ' + value)
  }

  listItem = ({item}) => (
    <View style={{flex: 1, flexDirection: 'row', justifyContent: 'space-between'}}>
      <Text style={styles.item}>{item.key}</Text>
      <Switch
        onValueChange={(value) => this.setState({switchValue: value})}
        value={this.state.switchValue}
      />
    </View>
  );

  render() {
    return (
      <FlatList
        data={listKeys}
        renderItem={this.listItem}
      />
    );
  }
}

const styles = StyleSheet.create({
  container: {
   flex: 1,
   paddingTop: 22
  },
  item: {
    padding: 10,
    fontSize: 18,
    height: 44,
  },
})

export default InterestsList;

回答1:


You can try this:

import React, { Component } from 'react';
import { FlatList, StyleSheet, Text, View, Switch } from 'react-native';


class InterestsList extends Component {
  constructor() {
    super();
    this.state = {
       listKeys: [
      {key: 'Basketball', switch : false},
      {key: 'Football', switch : false},
      {key: 'Baseball', switch : false},
      {key: 'Soccer', switch : false},
      {key: 'Running', switch : false},
      {key: 'Cross Training', switch : false},
      {key: 'Gym Workout', switch : false},
      {key: 'Swimming', switch : false},
    ]
    }
  }

  setSwitchValue = (val, ind) => {
      const tempData = _.cloneDeep(this.state.listKeys);
      tempData[ind].switch = val;
      this.setState({ listKeys: tempData });
  }

  listItem = ({item, index}) => (
    <View style={{flex: 1, flexDirection: 'row', justifyContent: 'space-between'}}>
      <Text style={styles.item}>{item.key}</Text>
      <Switch
        onValueChange={(value) => this.setSwitchValue(value, index)}
        value={item.switch}
      />
    </View>
  );

  render() {
    return (
      <FlatList
        data={this.state.listKeys}
        renderItem={this.listItem}
      />
    );
  }
}

const styles = StyleSheet.create({
  container: {
   flex: 1,
   paddingTop: 22
  },
  item: {
    padding: 10,
    fontSize: 18,
    height: 44,
  },
})

export default InterestsList;


来源:https://stackoverflow.com/questions/53368506/react-native-how-to-use-flatlist-to-create-multiple-switches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!