首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Picker.Item未定义的react本机

Picker.Item未定义的react本机
EN

Stack Overflow用户
提问于 2021-07-01 14:46:15
回答 1查看 41关注 0票数 1

我在我的项目中实现了https://github.com/react-native-picker/picker作为我自己的组件。为了在picker中拥有动态数量的项目,我决定用MyPicker.Item做一些类似这样的事情:

代码语言:javascript
复制
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>

这就是MyPicker的看法:

代码语言:javascript
复制
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>
        
    )
}

它正在工作,我可以选择这个项目,它返回适当的值,但我收到了这个警告和它的恼人。我该怎么处理呢?

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-01 15:11:59

在您的代码中没有定义MyPicker.Item -而且您实际上似乎只想重用在Picker.Item中定义的定义。一种可能的方法是:

代码语言:javascript
复制
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 };
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68205492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档