From 2e4ab9ff70713386a951771e6096e55fdd78e3be Mon Sep 17 00:00:00 2001 From: Atticus White Date: Sat, 10 Sep 2016 21:39:01 -0700 Subject: [PATCH] Bugfix - Only add valid IOSPicker items. Summary: Fixes https://github.com/facebook/react-native/issues/9216. As nickzuber describes in #9216, conditional `Picker.Item` elements will lead to exceptions downstream when the `Picker` attempts to construct the collection of items. [In the picker source](https://github.com/facebook/react-native/blob/a2fb703bbb988038323c55b29b40e8f5ff52966d/Libraries/Components/Picker/PickerIOS.ios.js#L48-L53) we can see that `child.props` is accessed when `child` has the potential to be an invalid `React` element. ```js 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}); }); ``` This change ensures the incoming element is valid ```diff ReactChildren.forEach(props.children, function (child, index) { + if (!React.isValidElement(child)) { + return; + } if (child.props.value === props.selectedValue) { selectedIndex = index; } items. Closes https://github.com/facebook/react-native/pull/9243 Differential Revision: D3847514 Pulled By: spicyj fbshipit-source-id: f46fbd4b0f81de7a92e1ca3e60b5ed15a9cbbf78 --- Libraries/Components/Picker/PickerIOS.ios.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/Components/Picker/PickerIOS.ios.js b/Libraries/Components/Picker/PickerIOS.ios.js index 1ff658c19..8141120be 100644 --- a/Libraries/Components/Picker/PickerIOS.ios.js +++ b/Libraries/Components/Picker/PickerIOS.ios.js @@ -45,7 +45,7 @@ var PickerIOS = React.createClass({ _stateFromProps: function(props) { var selectedIndex = 0; var items = []; - ReactChildren.forEach(props.children, function (child, index) { + ReactChildren.toArray(props.children).forEach(function (child, index) { if (child.props.value === props.selectedValue) { selectedIndex = index; }