[英]How to Reset react-native-picker-select on form submit using formik inReact Native?
I am using Formik and react-native-picker-select, I can reset text and number fields after submission but can not able to reset picker-select.我正在使用 Formik 和 react-native-picker-select,我可以在提交后重置文本和数字字段,但无法重置 picker-select。 How can I achieve it?
我怎样才能实现它?
The reseted value for react-native-picker-select
is null. react-native-picker-select
的重置值为 null。
So you need to create a state that stores the current value of the picker and when you want to reset it, set it to null.所以你需要创建一个 state 来存储选择器的当前值,当你想重置它时,将它设置为 null。
The secret here is set the value={state variable}
parameter, so that the picker updates every time that the state is also updated.这里的秘诀是设置
value={state variable}
参数,这样每次 state 也更新时选择器也会更新。 The state variable
is updated with onValueChange
. state variable
随onValueChange
更新。
const Component = () => {
const [pickerValue, setPickerValue] = useState(null);
return (
<>
<RNPickerSelect
onValueChange={v => setPickerValue(v)}
value={pickerValue}
items={[
{label: 'Football', value: 'football'},
{label: 'Baseball', value: 'baseball'},
{label: 'Hockey', value: 'hockey'},
]}
/>
<TouchableOpacity onPress={() => setPickerValue(null)}>
<Text>Submit</Text>
</TouchableOpacity>
</>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.