picker icon indicating copy to clipboard operation
picker copied to clipboard

Picker.Item crashes if label is type number without giving any error

Open robertwt7 opened this issue 5 years ago • 4 comments

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

robertwt7 avatar Nov 27 '20 00:11 robertwt7

Also happens when label is undefined even tho the type of label signals that it accepts undefined values.

EDIT: only tested on android btw

NilsKrause avatar Nov 27 '20 14:11 NilsKrause

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.

NilsKrause avatar Nov 27 '20 14:11 NilsKrause

Can be closed I think @Naturalclar

NilsKrause avatar Dec 02 '20 07:12 NilsKrause

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.

erquhart avatar Apr 18 '22 17:04 erquhart