使用"react-native-single-choice"库的步骤如下:
npm install react-native-single-choice --save
import React, { useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import SingleChoice from 'react-native-single-choice';
const data = [
{ label: 'Option 1', value: 'option1' },
{ label: 'Option 2', value: 'option2' },
{ label: 'Option 3', value: 'option3' },
];
const App = () => {
const [selectedValue, setSelectedValue] = useState('');
const handleSelect = (value) => {
setSelectedValue(value);
};
return (
Choose an option:
Selected value: {selectedValue}
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
label: {
fontSize: 16,
fontWeight: 'bold',
marginBottom: 10,
},
selectedValue: {
marginTop: 20,
},
});
export default App;
在组件中使用SingleChoice
组件,将选项数据作为data
属性传递,并使用selectedValue
属性指定当前选中的值。
创建一个处理选中事件的回调函数,当选中的值发生变化时,更新selectedValue
的状态。
在渲染的视图中,显示当前选中的值。
这样,你就可以在不使用hooks的情况下使用"react-native-single-choice"库了。