【问题标题】:React Native - CheckBox unable to uncheck the "checked" boxReact Native - CheckBox无法取消选中“选中”框
【发布时间】:2019-06-04 07:27:42
【问题描述】:

我已经使用 React native 一个月了,但这是我第一次在我的应用程序中使用 CheckBox。所以,最近我一直在努力检查 Flatlist 中的特定复选框,但现在我可以了。

但是在测试我的复选框时,我确实注意到,一旦我选中了一个特定的 CheckBox(或多个复选框),它就不会UNCHECK。

所以,我的目标是制作一个 CheckBox,它可以检查(当然)也可以取消选中,如果用户误选或误按 CheckBox。

这是我的代码

export default class tables extends Component {
    constructor(props){
        super(props)
        this.state = {
            ...
            check: false
        }
    }
    checkBox_Test = (item, index) => {
        let { check } = this.state;
        check[index] = !check[index];
        this.setState({ check: check })

        alert("now the value is " + !this.state.check);
        alert("now the value is " + item.tbl_id);
        console.log(item.tbl_id)
    }
    render() {
        return(
             <View>
                  ....
                  <Flatlist
                        ....
                        <CheckBox
                           value = { this.state.check }
                           onChange = {() => this.checkBox_Test(item) }
                        />
                        ....
                  />
             <View/>
        )
    }
}

【问题讨论】:

    标签: javascript android reactjs react-native checkbox


    【解决方案1】:

    方法一:勾选object

    export default class tables extends Component {
    constructor(props){
        super(props)
        this.state = {
            ...
            check: {}
        }
    }
    checkBox_Test = (id) => {
        const checkCopy = {...this.state.check}
        if (checkCopy[id]) checkCopy[id] = false;
        else checkCopy[id] = true;
        this.setState({ check: checkCopy });
    }
    render() {
        return(
             <View>
                  ....
                  <Flatlist
                        ....
                        <CheckBox
                           value = { this.state.check[item.tbl_id] }
                           onChange = {() => this.checkBox_Test(item.tbl_id) }
                        />
                        ....
                  />
             <View/>
        )
    }
    }
    

    方法2:为每个FlatList项制作一个单独的项

    class ListItem extends Component {
      constructor(props){
        super(props)
        this.state = {
            ...
            check: false
        }
     }
    
      checkBox_Test = (id) => {
        this.setState((prevState) => ({ check: !prevState.check }));
      }
    
      render() {
        return(
             <View>
               <CheckBox
                  value = { this.state.check }
                  onChange = { this.checkBox_Test }
               />
             </View>
        )
       }
     }
    

    让我知道它是否适合你

    【讨论】:

    • 谢谢先生! :) 方法 1 在我的情况下效果很好。因此,如果我可能会问,您是否知道我们如何通过已选中复选框的项目?就像我检查表号:1,然后将其传递到下一个屏幕。
    • @PurpleViolet 您可以将所选项目存储在类变量中,例如 this.selectedItem = item;当复选框为真时。现在您可以随时随地使用 selectedItem。
    • 对我来说,第一种方法行不通。将console.log 添加到checkBox_Test 时,我只会在第一次更改时看到输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 2016-05-28
    • 2015-10-29
    • 1970-01-01
    • 2017-04-14
    • 2019-10-15
    • 1970-01-01
    相关资源
    最近更新 更多