question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CheckBoxes not responding in List

See original GitHub issue

What’s the proper way to handle a list of checkboxes? I’ve the following code, but the checkboxes just don’t respond to my clicks on both iOS and Android emulators.

I have:

"native-base": "2.1.3",
"react": "16.0.0-alpha.3",
"react-native": "0.43.2", 

Here is the code:

import React, { Component } from 'react';
import { Container, Content, Item, List,
         CheckBox, Body, ListItem, Text } from 'native-base';

class SelectFriends extends Component {
  constructor(props) {
    super(props);

    this.state = {
      friends: [{ id: 10, name:"Gundam" },
                { id: 20, name:"Rambo" },
                { id: 30, name:"Mickey Mouse" } ],
      checkBoxChecked: false
    };
  }


  onCheckBoxPress(id) {
    console.log( "Checked!" );
    console.log( id );
    this.setState({
      checkBoxChecked: !this.state.checkBoxChecked
    });
  }

  render() {
    return(
      <Container>
        <Content>
          <Item>
            <List dataArray={this.state.friends}
              renderRow={(friend) =>
                <ListItem>
                  <CheckBox
                    checked={this.state.checkBoxChecked}
                    onPress={()=>this.onCheckBoxPress(friend.id)}
                  />
                  <Body>
                    <Text>{friend.name}</Text>
                  </Body>
                </ListItem>
              }
            >
            </List>
          </Item>
        </Content>
      </Container>
    );
  }
};

export default SelectFriends;

The console.log works on clicks… so it’s somewhat working…

I’ve tried nativebase version 2.1.4 and 2.1.5. Got tons of build errors, so I went back to 2.1.3…

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
hszetocommented, Jun 28, 2017

Thanks @shivrajkumar . I made some tweaking to your code… the checkboxes work now.

import React, { Component } from 'react';
import { ListView } from 'react-native';
import { Container, Content, Item, List,
         CheckBox, Body, ListItem, Text } from 'native-base';

class SelectFriends extends Component {
  constructor(props) {
    super(props);

    this.state = {
      friends: [{ id: 10, name:"Gundam" },
                { id: 20, name:"Rambo" },
                { id: 30, name:"Mickey Mouse" } ],
      selectedFriendId: []
    };
  }


  onCheckBoxPress(id) {
    let tmp = this.state.selectedFriendId;

    if ( tmp.includes( id ) ) {
      tmp.splice( tmp.indexOf(id), 1 );
    } else {
      tmp.push( id );
    }

    this.setState({
      selectedFriendId: tmp
    });
  }

  render() {
    return(
      <Container>
        <Content>
          <Item>
            <ListView
              enableEmptySections={true} 
              dataSource={ds.cloneWithRows(this.state.friends)}
              renderRow={(friend) =>
                <ListItem>
                  <CheckBox
                    checked={this.state.selectedFriendId.includes(friend.id) ? true : false}
                    onPress={()=>this.onCheckBoxPress(friend.id)}
                  />
                  <Body>
                    <Text>{friend.name}</Text>
                  </Body>
                </ListItem>
              }
            />
          </Item>
        </Content>
      </Container>
    );
  }
};

export default SelectFriends;
6reactions
teddytehcommented, Jan 23, 2018

For those who have an issue with Flatlists, add this as a prop to Flatlist itself: extraData={this.state}

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - checkbox is not working properly in html how to fix?
Try this. First select the check box in JavaScript: const $checkbox = document.querySelector('.
Read more >
Checkbox not working in lists - Honeycode Community
Hey Honeycode team, Checkboxes don't seem to work as described on Add Toggles: Checkboxes and Switches. I have a simple TODO list table ......
Read more >
Insert checkbox in Excel: create interactive checklist or to-do list
Although the checkboxes are inserted and you can now check or uncheck them by simply clicking on a box, Microsoft Excel is not...
Read more >
checkbox not getting selcted but list view shows exact value
i have some 5 checkboxes in form and they set to true base on some conditions of other fields. sometimes the checkboxes are...
Read more >
<input type="checkbox"> - HTML: HyperText Markup Language
Note: If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found