Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a react-native checkbox example?

Tags:

react-native

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

like image 450
Greg Avatar asked Nov 18 '17 03:11

Greg


People also ask

How do I manage checkboxes in react-native?

The core react-native package does not have checkbox support and you need to install a package to work with it. The value that can be given to status are checked, unchecked and indeterminate. The value is boolean.It can be used to enable/disable the checkbox.

How do I code a checkbox in React?

Using the updater function This handler will call the updater function setIsChecked for every input change with the latest input value. const Checkbox = ({ label }) => { // ... return ( <div className="checkbox-wrapper"> <label> <input // ... onChange={() => setIsChecked((prev) => !

How do you check checkbox is checked or not in react-native?

Use the target. checked property on the event object to check if a checkbox is checked in React, e.g. if (event. target. checked) {} .


2 Answers

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

like image 118
magneticz Avatar answered Sep 25 '22 02:09

magneticz


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
like image 22
Allef Douglas Avatar answered Sep 25 '22 02:09

Allef Douglas