React Native Custom Checkbox Component iOS Android Example

As we all know react native just launch its new 0.58 stable version but there is still a lot’s of components are missing and one of them is Checkbox. React native currently dose not provide us Checkbox component so we need to make our own custom checkbox. It is a two states handler button which can be checked and unchecked. Checkbox is used to get same type of multiple values from user in both android and iOS applications. Checkbox is very popular among developers because of its functionality to get multiple values from user. In this tutorial we would create Custom Checkbox Component for both iOS Android applications in react native.

Contents in this project React Native Custom Checkbox Component Example iOS Android:

1. We are handling the Checkbox checked functionality using a check image. So before going further please download the check image from below and put inside Your_React_Native_Project -> assets -> check.png . You need to manually create the assets folder in your react native project.

2. Open your project’s main App.js file and import Alert, Image, Platform, StyleSheet, Text, TouchableHighlight, and View component in your project.

3. Import PropTypes from prop-types in your project. We would create our custom props for Checkbox component. To use custom props in our project we need to import this. This library is come with react native project itself.

4. Create a new class named as Selected_Items_Array. This class is used to manage the selected checkbox items into array.

pushItem : Method is used to push selected items into array.

getArray : returns us the selected items in array format.

5. Create another class named as Checkbox. This class is our custom checkbox component class.

6. Create constructor method in Checkbox class. We are creating a state named as checked, this state is used to manage the checked and unchecked checkbox component states.

7. Create componentWillMount() method in Checkbox class. Here we would check the state value and according to that call the pushItem() function of Selected_Items_Array class using props. Here we would Create 3 item value for Checkbox.

  1. key : Must be defined and should not be same. Managed by this.props.keyValue custom prop.
  2. label : The text shows with checkbox. Managed by this.props.label prop.
  3. value : Value which you want to get on checkbox selection. Managed by this.props.value prop.

8. Create a function named as toggleState() in Checkbox class. This function is used to manage the checkbox checked and unchecked state and also insert values in array when checkbox is checked.

9. Creating the Checkbox view using TouchableHighlight component in render’s return block in in Checkbox class. We would call the toggleState function on onPress event of TouchableHighlight.

  • this.props.size : Used to give custom width and height of checkbox component.
  • this.props.color : Used to give the custom color of Checkbox.
  • this.props.labelColor : Used to give the custom checkbox label color.
  • this.props.label : Used to give the label of checkbox.

10. Complete source code for in Checkbox class:

11. Create Constructor() in your main export default App class.

  • selectedArrayOBJ : Object of Selected_Items_Array class.
  • selectedItems : This state is used to print all the selected items of checkbox in Text component for demonstration purpose.

12. Create a function named as getSelectedItems() in your main export default App class. This function will call on The button on-press event and we would retrieve all the selected checkbox values in this function and store in selectedItems state.

13. Create 3 Checkbox component in render’s return block of your main export default App class.

The Checkbox component has 8 different type of props available to modify it :

  1. size : Pass the value in number format. Set size of checkbox component.
  2. keyValue : Should not be same and used to manage the each checkbox individually in array.
  3. selectedArrayObject : Pass the object of Selected_Items_Array class.
  4. checked : By default check and un-check the checkbox. Support Boolean values.
  5. color : Pass the color in HEX color code. Used to change the color of checkbox.
  6. labelColor : Used to change the color of checkbox label color.
  7. label : Show the label text of checkbox.
  8. value : Which value you want to get on checkbox selection.

14. Creating Style.

15. Defining props default values and their data holder type.

16. Complete source code for App.js File :

Screenshots:

Custom Checkbox Component

8 Comments

  1. nice tutorial sir

  2. hii sir, make a tutorial on when scroll down to hide search bar and when scroll up to show search bar with flatList

  3. Hi ..let say i have three check box i.e. Android,Ios and Both
    We can change the state of check box one at a time.If i click on both then rest two check box will be checked.How to do that? please help

  4. Hi thanks for your reply.
    I have three options as check box
    1> Android
    2> Ios
    3> Both

    When we select option no 3 then option no 1 and option no 2 will
    also select .
    But i m unable to do that.Please help.

  5. But how to add both values.I am new in React
    toggleState(key, label, value) {

    if(value==’Both’){
    // can’t add logic here.Please add some code snippet
    }
    else{
    this.setState({ checked: !this.state.checked }, () => {
    if (this.state.checked) {
    this.props.selectedArrayObject.pushItem({ ‘key’: key, ‘label’: label, ‘value’: value });

    }
    else {
    this.props.selectedArrayObject.getArray().splice(this.props.selectedArrayObject.getArray().findIndex(x => x.key == key), 1);
    console.log(‘unchecked’)
    }
    });
    }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *