19

Are there any simple examples of implementing a react-native CheckBox? Facebook does not provided any examples. The CheckBox component that I am referring to can be found in the documentation here: http://facebook.github.io/react-native/docs/checkbox.html

1
  • 1
    The checkbox provided by react native is just a checkbox UI. If you want one just as we use in web, check out this library github.com/Rinkuk1993/rn-checkbox-list Commented Jan 16, 2020 at 11:42

8 Answers 8

27

Currently, the CheckBox component is only supported on Android as it is stated here. You should use the Switch component for iOS.

For Android, usage is pretty straight forward:

<View style={{ flexDirection: 'column'}}>
  <CheckBox />
  <View style={{ flexDirection: 'row' }}>
    <CheckBox
      value={this.state.checked}
      onValueChange={() => this.setState({ checked: !this.state.checked })}
    />
    <Text style={{marginTop: 5}}> this is checkbox</Text>
  </View>
</View>

View and Text components are optional, just to show how CheckBox can be used along with those. Code above will produce this screen on Android device:

Screenshot of the above code

This is how the above code appears on an iOS device:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

oh - ok - didn't seem to mention it on the doc page
yeah, react-native docs are usually under-documented. Best approach is to go to github issues and search there
Switch looks very different from a CheckBox.
24

You can create your own.

You will need to install react-native-vector-icons (or you can use images).

yarn add react-native-vector-icons
react-native link react-native-vector-icons

Creating file components/CheckBox/index.js

import React from 'react'
import Icon from 'react-native-vector-icons/MaterialIcons'

import styles from './styles'

import { TouchableOpacity, Text } from 'react-native'

const CheckBox = ({ selected, onPress, style, textStyle, size = 30, color = '#211f30', text = '', ...props}) => (
    <TouchableOpacity style={[styles.checkBox, style]} onPress={onPress} {...props}>
        <Icon
            size={size}
            color={color}
            name={ selected ? 'check-box' : 'check-box-outline-blank'}
        />

        <Text style={textStyle}> {text} </Text>
    </TouchableOpacity>
)

export default CheckBox

Creating file components/CheckBox/styles.js

import { StyleSheet } from 'react-native'

const styles = StyleSheet.create({
    checkBox: {
        flexDirection: 'row',
        alignItems: 'center'
    }
})

export default styles

How to usage

import React, { Component } from 'react'

import styles from './styles'

import { CheckBox } from '../../components'

import { View } from 'react-native'

class Signup extends Component {

    state = {
        termsAccepted: false
    }

    handleCheckBox = () => this.setState({ termsAccepted: !this.state.termsAccepted })

    render() {
        return (
            <View style={{}}>
                <CheckBox 
                    selected={this.state.termsAccepted} 
                    onPress={this.handleCheckBox}
                    text='Accept terms and conditions'
                />               
            </View>
        )
    }
}

export default Signup

1 Comment

Exactly. If not exists, we can create.
9

For starters who are not understood the above answers

import React, { Component } from 'react';
import { Platform, View, Text, CheckBox } from 'react-native';


var tempCheckValues = [];
export default class App extends Component {

  constructor(props) {
    super(props);

    this.state = {
      checkBoxChecked: []
    }
  }

  checkBoxChanged(id, value) {

    this.setState({
      checkBoxChecked: tempCheckValues
    })

    var tempCheckBoxChecked = this.state.checkBoxChecked;
    tempCheckBoxChecked[id] = !value;

    this.setState({
      checkBoxChecked: tempCheckBoxChecked
    })

  }

  render() {

    const products = [{
      id: 1
    },
    {
      id: 2
    },
    {
      id: 3
    }];

    return (

      products.map((val) => {

        { tempCheckValues[val.id] = false }

        return (

          <View key={val.id} style={{ flexDirection: 'column' }}>

            <CheckBox

              value={this.state.checkBoxChecked[val.id]}

              onValueChange={() => this.checkBoxChanged(val.id, this.state.checkBoxChecked[val.id])}

            />

          </View >

        )

      }

      )

    );
  }
}

1 Comment

Nice explanation! What is the key={val.id} in View? I did not find key as a props for View component in react native 0.59.
5

Using react-native-elements, the mission is easier and examples are available :

 import { CheckBox } from 'react-native-elements'

<CheckBox
  title='Click Here'
  checked={this.state.checked}
/>

<CheckBox
  center
  title='Click Here'
  checked={this.state.checked}
/>

<CheckBox
  center
  title='Click Here'
  checkedIcon='dot-circle-o'
  uncheckedIcon='circle-o'
  checked={this.state.checked}
/>

<CheckBox
  center
  title='Click Here to Remove This Item'
  iconRight
  iconType='material'
  checkedIcon='clear'
  uncheckedIcon='add'
  checkedColor='red'
  checked={this.state.checked}
/>

5 Comments

understood - I'm just trying to get the "native" react-native "CheckBox" component working here....
@Greg did you manage to do anything about it ? Im struggling with checkbox in react-native 0.49 aswell.. it seems to work ONCE only and then the state does not change back.
Seems there isn't one in react native itself
Where is the value for each checkbox? There is only title of checkbox.
Do I need an entire components library for a checkbox?
2

Checkbox has been removed from react-native, you can install and import it from @react-native-community/checkbox. https://github.com/react-native-checkbox/react-native-checkbox

Comments

1

You can also try this git repo that provides the checkbox and checkbox list. Try this - https://github.com/Rinkuk1993/rn-checkbox-list

Comments

0

checkbox and radiobox group

import { MaterialIcons } from "@expo/vector-icons";

const [genderIndex, setGenderIndex] = useState(0);   
const genderList = ["Male", "Female", "Other"];

const genderChangeHandler = 
(index) => {
  // console.log("index \t", index);
  setGenderIndex((preIndex) => index);
}


 <View style={{ flexDirection: "row" }}>
                    {genderList.map((data, index) => (
                      <TouchableOpacity
                        key={data}
                        style={{
                          flexDirection: "row",
                          margin: 10,
                          flex: 3,
                          justifyContent: "space-evenly",
                        }}
                        onPress={genderChangeHandler.bind(this, index)}
                      >
                        <MaterialIcons
                          name={
                            index === genderIndex
                              ? "radio-button-checked"
                              : "radio-button-unchecked"
                          }
                          size={18}
                          color='#ccc'
                        />
                        <Text style={styles.termsText}>{data}</Text>
                      </TouchableOpacity>
                    ))}
</View> 

Comments

-3

You can make customize button using state facility in react native instead of using checkbox or picker.

https://jsfiddle.net/kyazbs3j/2/

<TouchableOpacity onPress={() =>this.setState({stateExample:'Good'})}>
   <View style={this.state.stateExample=='Good styles.chooseItem:styles.chooseItem1'}>
       <Text style={styles.choosetxt}>Good</Text>
   </View>
</TouchableOpacity>

example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.