Skip to content

IOSPicker Children Bug #9216

@nickzuber

Description

@nickzuber

React-Native v0.20.0^

Question regarding intended behavior for the IOSPicker component.

The picker component assumes that its children are all React elements and does not do any sort of check when attempting to read children properties.

Source code I'm referring to:

ReactChildren.forEach(props.children, function (child, index) {
  if (child.props.value === props.selectedValue) {
    selectedIndex = index;
  }
  items.push({value: child.props.value, label: child.props.label});
});

The important thing to look at is child.props.value -- this assumes that a child element has a props property and checks its value which will throw an error if props doesn't exist.

This prevents us from performing lazy evaluations on Picker children:

<Picker ...>
  {someFalseCondition &&
    <Picker.Item label="conditionalOption" value="0" />
  }
  <Picker.Item label="option1" value="1" />
  <Picker.Item label="option2" value="2" />
  ...
</Picker>

Since someFalseCondition will return false, we end up with:

<Picker ...>
  {false}
  <Picker.Item label="option1" value="1" />
  <Picker.Item label="option2" value="2" />
  ...
</Picker>

Which will throw an error since the Picker component assumes all of the children have properties.

Is this intended behavior? Seems like a bug to me.. I feel like we should be able to do things like this. If this is a bug, I'll gladly make a PR to fix this issue.

RN playground example

screen shot 2016-08-04 at 12 07 06 pm

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions