Home > Web Front-end > JS Tutorial > How to show checkbox in reactnative?

How to show checkbox in reactnative?

WBOY
Release: 2023-08-27 22:57:01
forward
1233 people have browsed it

Checkbox is a common component that we often use on UI. We do have some cool ones How to display checkbox in reactnative.

The core react-native package does not support checkboxes, you need to install one software package to use it.

The following packages must be installed to display checkboxes-

npm install --save-dev react-native-paper
Copy after login

The basic checkbox components are as follows-

<Checkbox status={checkboxstatus} onPress={onCheckboxCheckedfunc} />
Copy after login

Now let’s look at some of the checkboxes Important Properties-

##statusThe value that can be set to give the status is Checked, unchecked, and undefined. DisabledThe value is a boolean. Can be used as Enable/disable checkbox. onPressFunction that will be called when the button is pressed The checkbox is checked. ColorThe color assigned to the checkboxuncheckColor
PropsDescription

< /td>Unchecked checkbox s color
This is a simple check box display -

useState is used to set the checked and unchecked status of the check box: display Below -

const [checked, setChecked] = React.useState(false);
Copy after login

status is saved in checked variable, setChecked method is used Update it.

When the user checks/unchecks the checkbox, the checked status will be updated as shown in the figure Below -

onPress={() => {
   setChecked(!checked);
}}
Copy after login

The complete code is as follows -

Example

import * as React from &#39;react&#39;;
import { StyleSheet, Text, SafeAreaView } from &#39;react-native&#39;;
import { Checkbox } from &#39;react-native-paper&#39;;
const MyComponent = () => {
   const [checked, setChecked] = React.useState(false);
   return (
      <SafeAreaView style={styles.container}>
         <Checkbox
            status={checked ? &#39;checked&#39; : &#39;unchecked&#39;}
            onPress={() => {
               setChecked(!checked);
            }}
            color={&#39;green&#39;}
            uncheckColor={&#39;red&#39;}
         />
      <Text>Checkbox</Text>
      </SafeAreaView>
   );
};
const styles = StyleSheet.create({
   container: {
      flex: 1,
      justifyContent: &#39;center&#39;,
      alignItems: &#39;center&#39;
   },
});
export default MyComponent;
Copy after login
Output

How to show checkbox in reactnative?

The above is the detailed content of How to show checkbox in reactnative?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template