简体   繁体   English

React Native Elements 复选框不断选择所有项目,而不是选择一个项目

[英]React Native Elements Checkbox keeps selecting all items instead of the one item this is selected

Hope all is well, and that everyone has a great and safe weekend.希望一切都好,每个人都有一个美好而安全的周末。 I need a little help with this one.我需要一点帮助。 I have been stuck on this for a week now.我已经坚持了一个星期了。 I have a react native project where I am using a flatlist with a react native element checkbox inside of it.我有一个 react native 项目,我在其中使用了一个带有 react native 元素复选框的 flatlist。 I have the flatlist displaying data fetched from my database.我有一个显示从我的数据库中获取的数据的平面列表。 So everything works correctly.所以一切正常。 But when I check one of the items that is being displayed, it checks them all.但是当我检查正在显示的项目之一时,它会检查所有项目。 I have googled and tried several different methods that I got off of here as well.我用谷歌搜索并尝试了几种不同的方法,这些方法也是我从这里得到的。 Either nothing is checked or all is checked in the checkbox.在复选框中没有选中任何内容或全部选中。 Can you guys please take a look at my code below and let me know if I am missing something?你们能不能看看我下面的代码,让我知道我是否遗漏了什么?

Thank you so much!!!非常感谢!!!

 import React from 'react'; import { View, Text, FlatList, StyleSheet, } from 'react-native'; import { ListItem, CheckBox, } from 'react-native-elements'; import BackButtonMGMT from '../Components/BackButtonMGMT'; export default class ViewCategory extends React.Component { constructor(props) { super(props); this.state = { dataSource: [], checked: false, } } render() { const { navigation } = this.props; const cust = navigation.getParam('food', 'No-User'); const other_param = navigation.getParam('otherParam', 'No-User'); const cust1 = JSON.parse(cust); const data = cust1; console.log(data); return ( <View style={styles.container}> <BackButtonMGMT navigation={this.props.navigation} /> <FlatList data={data} extraData={this.state} keyExtractor={(item, index) => index.toString()} renderItem={({ item, index }) => ( <CheckBox center titleProps={{ color: 'black', fontWeight: 'bold'}} title={item} iconRight checked={this.state.checked} size={30} onPress={() => this.setState({checked: !this.state.checked})} containerStyle={styles.checkBox} //bottomDivider //chevron /> )} /> </View> ) } onCheck = (item) => { this.setState((state) => ({ checked: !state.checked, })); } }

You are currently setting the checkboxes in the list to checked={this.state.checked}您当前正在将列表中的复选框设置为checked={this.state.checked}

It doesn't look like you're using the onCheck method and instead handling the event in the onPress .看起来您没有使用 onCheck 方法,而是在onPress处理事件。 So when your onPress event handler fires it is toggling the entire list because each item in the list is using the same state.因此,当您的 onPress 事件处理程序触发时,它会切换整个列表,因为列表中的每个项目都使用相同的状态。

Instead you need to keep track of all the items in the array's checked state.相反,您需要跟踪数组处于选中状态的所有项目。 You will want to do something like:你会想要做这样的事情:

this.state = {
  items: [],
  ... // other state
}
...
renderItem={({ item }) => (
  <CheckBox
    checked={!!item.checked} // <-- individual item checked state here
    onPress={() => {
      const items = [...this.state.items] // <-- shallow copy to show we're not mutating state
      const currentItemIndex = items.findIndex(v => v.name === item.name) // <-- lookup by something unique on the item like an ID. It's better to lookup rather than assume the array indexes are ordered the same.
      items[currentItemIndex].checked = !items[currentItemIndex].checked
      this.setState(state => ({ ...state, items }))
    }}
    ... // other props
  />
)}

This is how I solved it, but now I am not able to select multiple items at a time.这就是我解决它的方法,但现在我无法一次选择多个项目。

 export default class ViewCategory extends React.Component { constructor(props) { super(props); this.state = { dataSource: [], checked: null, } } render() { const { navigation } = this.props; const cust = navigation.getParam('food', 'No-User'); const other_param = navigation.getParam('otherParam', 'No-User'); const cust1 = JSON.parse(cust); const data = cust1; console.log(data); return ( <View style={styles.container}> <BackButtonMGMT navigation={this.props.navigation} /> <FlatList data={data} extraData={this.state} keyExtractor={(item, index) => index.toString()} renderItem={({ item, index }) => ( <CheckBox center titleProps={{ color: 'black', fontWeight: 'bold'}} title={item} iconRight checked={this.state.checked === item} size={30} onPress={() => this.setState({checked: item})} containerStyle={styles.checkBox} /> )} /> </View> ) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM