【问题标题】:Picker.Item undefined react nativePicker.Item 未定义反应原生
【发布时间】:2021-07-01 06:46:15
【问题描述】:

我在我的项目中将https://github.com/react-native-picker/picker 实现为我自己的组件。 为了在选择器中有动态数量的项目,我决定用 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>

这就是 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>
        
    )
}

它正在工作,我可以选择这个项目,它返回正确的值,但我收到这个警告并且它很烦人。我该如何处理?

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.

【问题讨论】:

  • MyPicker.Item 定义在哪里?
  • 没有定义。它来自&lt;Picker.Item label="Java" value="java" /&gt; &lt;Picker.Item label="JavaScript" value="js" /&gt;,在我的项目中,我在 MyPicker 组件中将它与 {props.children} 一起使用了如何定义它以使其正常工作?

标签: react-native picker react-native-picker


【解决方案1】:

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

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

【讨论】:

  • 我不能这样导出。 Declaration or statement expected。很抱歉这个简单的问题,但我是新来的反应原生。
  • 修复导出语句,再试一次。
猜你喜欢
  • 2019-07-30
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 2019-06-21
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
相关资源
最近更新 更多