Picker.Item undefined react native

696 Views Asked by At

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.
1

There are 1 best solutions below

2
On BEST ANSWER

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