picker
picker copied to clipboard
Picker.Item crashes if label is type number without giving any error
I have this component:
<View
style={{
backgroundColor: "white",
padding: 22,
}}
>
<Text h4>Choose form option below:</Text>
<Picker
selectedValue={field.value}
onValueChange={handleChange}
testID={name}
>
{options.map((item) => (
<Picker.Item
key={item.id}
label={item.name.toString()}
value={item.id}
/>
))}
</Picker>
</View>
If the label props is number, the app crashes without throwing any errors.. I have to convert it explicitly to string for <Picker.Item />, took me a while to realise that is the bug
Also happens when label is undefined even tho the type of label signals that it accepts undefined values.
EDIT: only tested on android btw
Ok after investigating a little I found that the prop type of Picker.Item is defined like this: https://github.com/react-native-picker/picker/blob/716b81628d157c03aa102250c68899ca085f6d13/js/PickerItem.js#L17
but in Picker.d.ts it is defined like this: https://github.com/react-native-picker/picker/blob/716b81628d157c03aa102250c68899ca085f6d13/typings/Picker.d.ts#L7
I'd propose to fix the type definition as it seems intended to have the label as a defined string.
Can be closed I think @Naturalclar
I just ran into this using a number as a label, took a while to figure out since the crash is truly silent. I don't use TypeScript, but I would expect this error to be caught and messaged out regardless. Perhaps complicated at the OS layer.
Additional context, I'm only testing on iOS Simulator via Expo at the moment.