最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native - CheckBox unable to uncheck the "checked" box - Stack Overflow

programmeradmin7浏览0评论

I've been using React native for a month now but it's my first time to use a CheckBox in my application. So, lately I've been struggling to check a specific checkbox inside a Flatlist but now I can.

But upon testing my checkboxs I did notice that once I check a specific a CheckBox(or more than 1 checkbox) it doesn't UNCHECK.

So, my goal is to make a CheckBox that can check(ofcourse) and also uncheck, if ever a user mischeck or mistap a CheckBox.

Here's my code

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

I've been using React native for a month now but it's my first time to use a CheckBox in my application. So, lately I've been struggling to check a specific checkbox inside a Flatlist but now I can.

But upon testing my checkboxs I did notice that once I check a specific a CheckBox(or more than 1 checkbox) it doesn't UNCHECK.

So, my goal is to make a CheckBox that can check(ofcourse) and also uncheck, if ever a user mischeck or mistap a CheckBox.

Here's my code

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

Share Improve this question edited Jan 9, 2019 at 14:27 Purple Violet asked Jan 9, 2019 at 13:43 Purple VioletPurple Violet 751 gold badge3 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Method 1: Make check an 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/>
    )
}
}

Method 2: Make a separate item for each FlatList item

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

Let me know if it works for you

发布评论

评论列表(0)

  1. 暂无评论