You can make the control a controlled control by handling the onValueChange
and setting the value
.
According to a github issue, setting the value to null
should reset the selection, but unfortunately that doesn't seem to work. I set the placeholder item value to be 0 so we can rest it correctly.
export default function App() {
const [value, setValue] = React.useState(0);
const clean = () => {
//reset picker
setValue(0) // <-- reset by setting the placeholder value.
}
return (
<View>
<RNPickerSelect
onValueChange={(value) => setValue(value)} // <-- set state manually.
placeholder={{
label: 'Select',
value: 0
}}
useNativeAndroidPickerStyle={false}
items={[
{ label: 'Option 1', value: '1' },
{ label: 'Option 2', value: '2' },
{ label: 'Option 3', value: '3' },
]}
value={value} // <-- use the value from state
/>
<TouchableOpacity
onPress={clean}>
<Text>Clean</Text>
</TouchableOpacity>
</View>
);
}
https://snack.expo.io/_SweQ21QA
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…