React Native Checkbox Component Example

React Native CheckBox Component Example Featured

Hello everyone, Welcome to my new article, where we will explore React Native CheckBox Component by creating a quiz app screen.
The concept is pretty simple and straightforward.
The Checkbox component is best used in Quiz apps, so to Demonstrate how it works, I think we should make one and explore from there.
So let’s get into it.

QUIZ App UI Concept

The example we are building will be pretty simple and clean.
Most of Quiz apps screens out there usually contain a heading text with the question of the quiz.
The list of potential answers selections, and a submit button.
So, This is what we will be building.

React Native Checkbox Component example Concept Ui App

Environment Setup

To get started as usual create a new project using Expo or React Native Cli.
With both approaches, you will usually get a starting code like this.

React Native Screen Checkbox component example Start screen
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

export default class App extends React.Component {
  render(){
    return (
      <View style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

We will also need to install native base, since we are going to use it’s Checkbox component.

To install native base, follow these instructions.

Install the package

npm install native-base --save

If you are using React Native Cli, one more step is required

react-native link

Now we are ready to build our quiz app example.

Getting Started

Import the Checkbox component from native base

import {CheckBox} from "native-base"

Also import the TouchableOpacity from React Native, since we have to add a submit button

import { StyleSheet, Text, View, TouchableOpacity } from 'react-native';

Our initial state will just have a single property to hold the users’ answer

  state={
    selectedLang:0
  }

in the root view, we want to add a header Text at the top with a bold and clean color, for the quiz question.

<Text style={styles.header}>What's your favorite programming language?</Text>

With the style below

  header:{
    fontSize:25,
    fontWeight:"bold",
    color:"#364f6b",
    marginBottom:40
  }

Checkbox Component

Now or the checkbox component part, we are going to have an item View to hold the checkbox and the text option.
We also want to make the text dynamic and changes color and thickness when its option is selected.
Plus the checkbox’s functionality update onPress and have a clean color.
To Achieve this, our code will look something similar to this.

<View style={styles.item} >
    <CheckBox checked={this.state.selectedLang===1} color="#fc5185" onPress={()=>this.setState({selectedLang:1})}/>
    <Text style={
      {...styles.checkBoxTxt,
        color:this.state.selectedLang===1?"#fc5185":"gray",
        fontWeight:this.state.selectedLang===1? "bold" :"normal"
      }}
      >Python</Text>
</View>
  item:{
    width:"80%",
    backgroundColor:"#fff",
    borderRadius:20,
    padding:10,
    marginBottom:10,
    flexDirection:"row",
  },
  checkBoxTxt:{
    marginLeft:20
  },

Add more checkbox items for each potential answer for the question.
in my case I will add 3 more.

And finally, a submit button with the same checkbox color and a simple submit text

<TouchableOpacity style={styles.submit}>
  <Text style={{color:"white"}}>SUBMIT</Text>
</TouchableOpacity>
  submit:{
    width:"80%",
    backgroundColor:"#fc5185",
    borderRadius:20,
    padding:10,
    alignItems:"center",
    marginTop:40
  }

And there you have it a simple yet elegant React Native Checkbox Component Example, by building a Quiz app screen.

Final Result

React Native Checkbox Component Example

I have prepared a github repository for the project and an expo project as well, feel free to use them at your will.

React Native Countdown Timer Example Using MomentJs
React Native Camera Expo Example
React Native Accessibilityinfo API
React Native Flatlist Example
React Native Screen Transitions

Total
20
Shares
Leave a Reply

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

Previous Post
React Native Screen Transitions featured

React Native Screen Transitions

Next Post
Multiple Select Checkbox In React Native Featured

Multiple Select Checkbox In React Native

Related Posts