我正在使用这个包Radio Button。它工作正常,但我需要重置选定的值,因为我在那里找不到任何解决方案。
单选按钮重置
IT技术
reactjs
react-native
2022-07-13 01:41:31
2个回答
您可以更改状态以呈现新的单选按钮 - https://snack.expo.dev/iiHkFYpLV
import React, { useState } from "react";
import { View, StyleSheet, Button, Alert } from "react-native";
import RadioButtonRN from 'radio-buttons-react-native';
const App = () => {
const [show,setShow] = React.useState(true);
const data = [
{
label: 'data 1'
},
{
label: 'data 2'
}
];
React.useEffect(()=>{
if(!show) setShow(true)
},[show])
const resetHandler = () =>{
setShow(false)
}
return (
<View style={styles.container}>
{show &&
<RadioButtonRN
data={data}
selectedBtn={(e) => console.log(e)}
/>
}
<Button title='reset' onPress={resetHandler} />
</View>
);
}
const styles = StyleSheet.create({
container: {
paddingTop:100,
}
});
export default App;
该软件包没有重置功能,但同一功能有一个 PR 待定。整个包仅在一个文件中,因此您可以将包文件复制到您的代码中并从 PR 添加与功能相关的代码。
或者你可以使用另一个包;)