简体   繁体   English

Picker.Item undefined 反应原生

[英]Picker.Item undefined react native

I implemented https://github.com/react-native-picker/picker in my project as a my own component.我在我的项目中实现了https://github.com/react-native-picker/picker作为我自己的组件。 To have dynamic number of items in picker I decided to do something like this with MyPicker.Item:为了在选择器中拥有动态数量的项目,我决定用 MyPicker.Item 做这样的事情:

import { MyPicker } from '../Common/MyPicker.component';


<MyPicker
   style={styles.picker}
   selectedValue={this.state.selectedItem}
   onValueChange={(itemValue) =>
     this.setState({selectedItem: itemValue})}>
   <MyPicker.Item label='dziewczynka' value='dziewczynka' color='black' />
   <MyPicker.Item label='chłopiec' value='chłopiec' color='black' />
</MyPicker>

and that is how MyPicker is looking:这就是 MyPicker 的样子:

import React, { Component } from 'react';
import {
    View
  } from 'react-native';
import {Picker} from '@react-native-picker/picker';
import styles from './MyPicker.component.style';

export function MyPicker(props) {
    return (
        <View style={styles.container}>
            <Picker
                onValueChange={(itemValue) =>
                    props.onValueChange(itemValue)}
                mode={'dropdown'}>
                {props.children}
            </Picker>
        </View>
        
    )
}

It is working, I can select this items, its returning proper value but I am receiving this warning and its irritating.它正在工作,我可以选择这个项目,它返回了正确的值,但我收到了这个警告并且很烦人。 How can I handle this?我该如何处理?

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

There's no definition for MyPicker.Item in your code - and it seems that you actually just want to reuse the one defined in Picker.Item .在您的代码中没有MyPicker.Item的定义 - 看来您实际上只是想重用Picker.Item定义的Picker.Item One possible approach:一种可能的方法:

import {Picker} from '@react-native-picker/picker';
// ...

function MyPicker(props) {
    return (
        <View style={styles.container}>
            <Picker
                onValueChange={(itemValue) =>
                    props.onValueChange(itemValue)}
                mode={'dropdown'}>
                {props.children}
            </Picker>
        </View>
    )
}

MyPicker.Item = Picker.Item;
export { MyPicker };

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

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