React Native Elements Checkbox会继续选择所有项目,而不是选中该项目

问题描述

希望一切都很好,每个人都度过一个愉快而安全的周末。我需要一点帮助。我已经坚持了一个星期。我有一个React Native项目,我在其中使用一个带有React Native元素复选框的平面列表。我有一个平面列表,显示数据库获取的数据。因此,一切正常。但是,当我检查正在显示的项目之一时,它会全部检查它们。我用谷歌搜索并尝试了几种我也从这里得到的不同方法。复选框中未选中任何内容或全部选中了该复选框。你们可以看看下面的我的代码,让我知道我是否缺少什么吗?

非常感谢您!

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,}));
    }
}

解决方法

您当前正在将列表中的复选框设置为checked={this.state.checked}

看起来您不是在使用onCheck方法,而是在onPress中处理事件。因此,当onPress事件处理程序触发时,它会切换整个列表,因为列表中的每个项目都使用相同的状态。

相反,您需要跟踪处于阵列检查状态的所有项目。您将想要做类似的事情:

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
  />
)}
,

这是我解决的方法,但是现在我一次不能选择多个项目。

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>
        )
    }