简体   繁体   English

警告:React.createElement:类型在React Native Picker中无效

[英]Warning: React.createElement: type is invalid with React Native Picker

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. 警告:React.createElement:类型无效-预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到:未定义。 You likely forgot to export your component from the file it's defined in. Check your code at AddCarScreen.js:30. 您可能忘记了从定义文件中导出组件。请在AddCarScreen.js:30中检查代码。 ... ...

I keep getting this error and it says to check my code at line 30 which is the line where it dynamically generates picker items based on the array given to state in the constructor. 我不断收到此错误,它说要在第30行检查我的代码,这是它根据给定构造函数中的状态数组动态生成选择器项的行。

Styles is a correctly imported with export default in a different file. 可以正确导入样式,并在其他文件中默认导出默认值。 This javascript file will then be exported to the central drawer navigator in a different file. 然后,此javascript文件将以其他文件的形式导出到中央抽屉导航器。

    import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View, Button, Picker } from 'react-native';
import styles from './StyleSheet.js';

export default class AddCarScreen extends Component {
  constructor(props){
super(props)

this.state = { 
  carBrand : "", 
  carBrandList : [ 'Ford', 'VW', 'Mazda' ]
}; 

} }

  static navigationOptions = {
      drawerLabel: 'Add Car',
  }



 render() {

const { navigate } = this.props.navigation;

return(
  <View style = { styles.container }> 
    <Text style = { styles.screenTitle }> Add Car </Text>
    <Picker
      selectedValue = {this.state.carBrand}
      onValueChange = {(itemValue) => this.setState({ carBrand: itemValue })}>
      **{ this.state.carBrandList.map((item, index) => { return <Picker.item label = {item} value = {item} key = {index}/> } ) }**
    </Picker>
    <Button 
      onPress = {() => navigate('DrawerOpen')}
      title = "Menu"
    />
  </View>
    );
  }
};

You should change Picker. 您应该更改Picker。 item to Picker. 项目到选择器。 Item as it is at official docs: https://facebook.github.io/react-native/docs/picker.html 项目 ,因为它是在官方的文档: https://facebook.github.io/react-native/docs/picker.html

I think this is a current bug with vanilla Picker. 我认为这是Vanilla Picker当前的错误。 I couldn't get a map working either! 我也无法使用地图! I also found countless questions similar to this. 我也发现了无数与此类似的问题。 In the meantime I couldn't wait and found this package and .map worked. 在此期间,我等不及了,发现这个包和.map可以工作了。 React-native-smart-picker . React-native-smart-picker

             <ScrollView >
                <View style={{ flex: 1, marginTop: 20 }}>
                    {this.state.models.length > 0 ?
                        <ScrollView >
                            <SmartPicker

                                expanded={this.state.expanded}
                                selectedValue={this.state.selectedModel}
                                label='Select Model'
                                onValueChange={this.handleChange.bind(this)}
                            >
                                {
                                    this.state.models.map((ele) => {
                                        return (<Picker.Item label={ele} value={ele} />)
                                    })
                                }
                            </SmartPicker>
                            <Button block onPress={() => this.props.vehicleModel(this.state.selectedModel)}>
                                <Text>Done</Text>
                            </Button>
                        </ScrollView>
                        : <Spinner />}

                </View>

            </ScrollView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM