This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).
Expo 复选框
一个提供基本复选框功能的通用 React 组件。
Android
iOS
tvOS
Web
Included in Expo Go
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
expo-checkbox 为所有平台提供了一个基础的 boolean 输入元素。
安装
Terminal
- npx expo install expo-checkbox用法
基础 Checkbox 用法
import { Checkbox } from 'expo-checkbox'; import { useState } from 'react'; import { StyleSheet, Text, View } from 'react-native'; export default function App() { const [isChecked, setChecked] = useState(false); return ( <View style={styles.container}> <View style={styles.section}> <Checkbox style={styles.checkbox} value={isChecked} onValueChange={setChecked} /> <Text style={styles.paragraph}>普通复选框</Text> </View> <View style={styles.section}> <Checkbox style={styles.checkbox} value={isChecked} onValueChange={setChecked} color={isChecked ? '#4630EB' : undefined} /> <Text style={styles.paragraph}>自定义颜色复选框</Text> </View> <View style={styles.section}> <Checkbox style={styles.checkbox} disabled value={isChecked} onValueChange={setChecked} /> <Text style={styles.paragraph}>禁用的复选框</Text> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, marginHorizontal: 16, marginVertical: 32, }, section: { flexDirection: 'row', alignItems: 'center', }, paragraph: { fontSize: 15, }, checkbox: { margin: 8, }, });
下面展示了 expo-checkbox 在 Android 和 iOS 上的示例:
API
import { Checkbox } from 'expo-checkbox';
Component
Type: React.Element<CheckboxProps>
Optional • Type:
ColorValueThe tint or color of the checkbox. This overrides the disabled opaque style.
Optional • Type:
booleanIf the checkbox is disabled, it becomes opaque and uncheckable.
Optional • Type:
(event: NativeSyntheticEvent<CheckboxEvent> | SyntheticEvent<HTMLInputElement, CheckboxEvent>) => voidCallback that is invoked when the user presses the checkbox.
event: NativeSyntheticEvent<CheckboxEvent> | SyntheticEvent<HTMLInputElement, CheckboxEvent>A native event containing the checkbox change.
Optional • Type:
(value: boolean) => voidCallback that is invoked when the user presses the checkbox.
value: booleanA boolean indicating the new checked state of the checkbox.
Optional • Type:
boolean • Default: falseValue indicating if the checkbox should be rendered as checked or not.