简体   繁体   中英

Picker.Item undefined react native

I implemented https://github.com/react-native-picker/picker in my project as a my own component. To have dynamic number of items in picker I decided to do something like this with 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:

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 . 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 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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