react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Picker - undefined is not an object (evaluating 'this.props.children[position].props)

Open vhoen opened this issue 10 years ago • 9 comments

I am encountering a weird behaviour when using Picker.

I use a Picker as follows :

<Picker
  mode="dropdown"
  style={styles.pickerField}
  selectedValue={this.state.dayAndTime}
  onValueChange={(text) => this.setState({ dayAndTime: text })}
>
  <Picker.Item label="Le 5/07 à 15H" value="0" key="0" />
</Picker>

When the screen displaying this picker is loaded, I got an error screen saying (cf. screenshot below) : undefined is not an object (evaluating 'this.props.children[position].props)

From what I gathered, my problem come from line 106 of Libraries/Components/Picker/PickerAndroid.android.js, it seems that onValueChange creates it, so I removed it, and I don't have the error anymore.

I use react-native 0.31.0, I use an android api 23 virtual device with genymotion.

Is there something I am missing ?

pickererror

vhoen avatar Aug 18 '16 15:08 vhoen

Error was triggered because there was only one Picker.Item, after I added one, everything went smoothly.

vhoen avatar Aug 19 '16 13:08 vhoen

@vhoen I get the same issue and I have several items and it's triggered when the value changed. I'm using react 0.33

renderEventPicker() {
    let { project } = this.props;
    let { report } = this.state;
    if(project.eventTypes.length > 0) {
      return (
        <Picker
          selectedValue={this.state.eventTypeId}
          onValueChange={(data) => {
            report.eventTypeId = data;
            report.eventCategoryId = null;
            this.setState({report});
          }}
        >
          <Picker.Item key={0} label={'Tipo de evento'} value={''} />
          {project.eventTypes.map((eventType) => <Picker.Item key={eventType.id} label={eventType.name} value={eventType.id} />)}
        </Picker>
      );
    }
    return null;
  }

Is there something I'm missing?

EDIT: This is the stack trace

undefined is not an object (evaluating '_this2.props.children[position].props.value')
_onChange
    PickerAndroid.android.js:144
invokeGuardedCallback
    ReactErrorUtils.js:26
executeDispatch
    EventPluginUtils.js:89
executeDispatchesInOrder
    EventPluginUtils.js:112
executeDispatchesAndRelease
    EventPluginHub.js:44
executeDispatchesAndReleaseTopLevel
    EventPluginHub.js:55
forEachAccumulated
    forEachAccumulated.js:27
processEventQueue
    EventPluginHub.js:231
runEventQueueInBatch
    ReactEventEmitterMixin.js:18
handleTopLevel
    ReactEventEmitterMixin.js:29
<unknown>
    ReactNativeEventEmitter.js:125
perform
    Transaction.js:138
batchedUpdates
    ReactDefaultBatchingStrategy.js:63
batchedUpdates
    ReactUpdates.js:98
_receiveRootNodeIDEvent
    ReactNativeEventEmitter.js:124
receiveEvent
    ReactNativeEventEmitter.js:138
__callFunction
    MessageQueue.js:229
<unknown>
    MessageQueue.js:111
guard
    MessageQueue.js:42
callFunctionReturnFlushedQueue
    MessageQueue.js:110

alexsotocx avatar Sep 14 '16 19:09 alexsotocx

Having the same issue as @alexsotocx, However if you add to static items, you can select them with no error, but any items dynamically added in a map throw the error reported

willoughby avatar Sep 22 '16 09:09 willoughby

@willoughby please refers to #9927

alexsotocx avatar Sep 22 '16 11:09 alexsotocx

@willoughby Same. Got a response and rendering it after setState. When value is changes i am thrown this error.

dkaushik94 avatar Mar 18 '17 12:03 dkaushik94

so, i found a resolution that we can create an array contains blank Item before use it.

render() {
    const pickerItems = this.props.depts.map(dept => {
      return (
        <Picker.Item key={'d' + dept.id} label={dept.name} value={dept.id} />
      )
    })

    pickerItems.unshift(<Picker.Item key="d0" label="选择部门" value="" />)

    return (
        <Picker
             selectedValue={this.state.deptId}
             onValueChange={value => this.setState({ deptId: value })}>
             {pickerItems}                  
        </Picker>
    )
}

luoyjx avatar Jun 24 '17 14:06 luoyjx

render() {
    const pickerItems = this.props.depts.map(dept => {
      return (
        <Picker.Item key={'d' + dept.id} label={dept.name} value={dept.id} />
      )
    })

    pickerItems.unshift(<Picker.Item key="d0" label="选择部门" value="" />)

    return (
        <Picker
             selectedValue={this.state.deptId}
             onValueChange={value => this.setState({ deptId: value })}>
             {pickerItems}                  
        </Picker>
    )
}

luoyjx avatar Jun 24 '17 14:06 luoyjx

I had the same issue and I fixed setting an empty array as the default value of my dataSource prop:

const defaultProps = {
	label: '',
	dataSource: []
};

const PickerWithLabel = (props) => {
	return (
		<View style={styles.container}>
			<Label style={styles.label}>{props.label}</Label>
			<Picker style={styles.picker} mode="dropdown" { ...props }>
				<Picker.Item label="--- Select ---" value="" />
				{
					props.dataSource.map(({ label, value }) => (
						<Picker.Item key={value} label={label} value={value} />
					))
				}
			</Picker>
		</View>
	)
}

PickerWithLabel.defaultProps = defaultProps;

rafaelgrilli92 avatar Dec 18 '17 11:12 rafaelgrilli92

I resolved my issue using the suggested workaround by @luoyjx! Thanks mate!

renderModalStatePicker = () => {
        const pickerItems = Object.keys(this.state.statelist).map((key) => {
            return (<Item label={this.state.statelist[key]} value={key} key={key}/>)
        })     
        pickerItems.unshift(<Item key="as" label="All States" value="as" />)

        return(
            <View>
                <ListItem>
                    <Text>States</Text>
                </ListItem>
                
                <Picker
                    mode="dropdown"
                    placeholder="Choose a state"
                    selectedValue={this.state.selectedState}
                    onValueChange={this.handleSelectState.bind(this)}>  
                    {pickerItems}
                </Picker>
            </View>
        );
    }

misheki avatar Mar 23 '18 01:03 misheki