Null check on Picker Items (#24057)

Summary:
when conditional rendering is used then picker breaks when the child is null in iOS

This conditional rendering inside Picker fails when "someBooleanValue" variable is false

```
{
   this.state.someBooleanValue && <Picker.Item label="value" value="value" />
}
```

[iOS] [Fixed] - null check on children by using filter
Pull Request resolved: https://github.com/facebook/react-native/pull/24057

Differential Revision: D14538337

Pulled By: cpojer

fbshipit-source-id: d9324671931b5f1dac65d8058d9aa957b650af25
This commit is contained in:
Rahul kishan M
2019-03-20 07:50:59 -07:00
committed by Facebook Github Bot
parent 80743eb7b1
commit aa10b3f293

View File

@@ -91,16 +91,18 @@ class PickerIOS extends React.Component<Props, State> {
static getDerivedStateFromProps(props: Props): State {
let selectedIndex = 0;
const items = [];
React.Children.toArray(props.children).forEach(function(child, index) {
if (child.props.value === props.selectedValue) {
selectedIndex = index;
}
items.push({
value: child.props.value,
label: child.props.label,
textColor: processColor(child.props.color),
React.Children.toArray(props.children)
.filter(child => child !== null)
.forEach(function(child, index) {
if (child.props.value === props.selectedValue) {
selectedIndex = index;
}
items.push({
value: child.props.value,
label: child.props.label,
textColor: processColor(child.props.color),
});
});
});
return {selectedIndex, items};
}