Skip to content

Commit

Permalink
Picker (android): Convert children to an array before accessing with …
Browse files Browse the repository at this point in the history
…a position

Summary:
When using the following component, `this.props.children` is not a flat array.

``` js
class Example extends Component {
    // ...

    render() {
        const values = ['1', '2'];

        return (
            <Picker
                value={this.state.value}
                onValueChange={this.onValueChange.bind(this)}
            >
                <Picker.Item
                    label="n/a"
                    value={null}
                />

                {values.map(value => {
                    return (
                        <Picker.Item
                            label={value}
                            value={value}
                        />
                    );
                })}
            </Picker>
        );
    }
}
```

The resulting `this.props.children` is:

``` js
[
    (child),
    [
        (child),
        (child),
    ],
];
```

Therefor you can't use `this.props.children[2]` to get the last item.

The Android version of the [Picker](https://facebook.github.io/react-native/do
Closes #8153

Differential Revision: D4753480

Pulled By: javache

fbshipit-source-id: deb0264746b39303e66c69c191af0c962db39085
  • Loading branch information
justim authored and facebook-github-bot committed Mar 23, 2017
1 parent 8a8f34a commit 720e195
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Libraries/Components/Picker/PickerAndroid.android.js
Expand Up @@ -116,7 +116,8 @@ class PickerAndroid extends React.Component {
if (this.props.onValueChange) { if (this.props.onValueChange) {
var position = event.nativeEvent.position; var position = event.nativeEvent.position;
if (position >= 0) { if (position >= 0) {
var value = this.props.children[position].props.value; var children = React.Children.toArray(this.props.children);
var value = children[position].props.value;
this.props.onValueChange(value, position); this.props.onValueChange(value, position);
} else { } else {
this.props.onValueChange(null, position); this.props.onValueChange(null, position);
Expand Down

0 comments on commit 720e195

Please sign in to comment.