【问题标题】:Render Error Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined渲染错误元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义
【发布时间】:2022-01-12 02:31:07
【问题描述】:

我正在尝试从 assets 文件夹中导入 react-native 上的图像(包含所有图像)。但是,当我在 IOS 模拟器上导出图像时,图像并没有编译。我认为添加我的assets 文件(包含所有图像)是一个只读文件很重要。我在资产文件夹的底部附加了一张图片。除了那张图片,我还附上了我在使用 IOS 模拟器时遇到的两个错误的屏幕截图。

感谢您的宝贵时间!

现在,为了更清楚,这里是我的代码:

import React from 'react';

import  { ImageBackround, StyleSheet, View, Image, Text} from 'react-native';

function WelcomeScreen(props){
    return(
        <>
        <ImageBackround
        style={styles.background}
        source={require('./assets/icon.png')}
        >
        <View style={styles.logoContainer}>

        <Image style={styles.logo} source={require('./assets/favicon.png')}/>
        <Text> Sell what you dont need!</Text>
        </View>
        <View style ={styles.registerButton}></View>
        < View style= {styles.loginButton}></View>
        </ImageBackround>
        </>
    )
}
const styles = StyleSheet.create({
    background: {
        flex:1,
        alignItems: "center" ,
    
    },
    loginButton: {
        width: "100%",
        height: 70,
        backgroundColor: "#4ecdc4",

    },
    logo: {
        width: 100,
        height: 100,

    },
    logoContainer: {
        position: "absolute",
        top: 70,
        alignItems:"center",
    
    },
    registerButton: {
        width: "100%",
        height: 70,
        backgroundColor: "#4ecdc4"
    },
});



export default WelcomeScreen;


这里按顺序附上assets 文件的图像,以及 Ios 模拟器给我的 2 个错误:

【问题讨论】:

    标签: javascript reactjs react-native ios-simulator render


    【解决方案1】:

    您能否尝试在资产中创建一个文件来导出所有图像并从您的 WelcomeScreen 调用它。

    像这样在 assets 文件夹中创建 index.js 文件

    export const Images ={
       adaptive:require('./adaptive.png'),
       favicon:require('./favicon.png'),
       icon:require('./icon.png'),
       splash:require('./splash.png')
    }
    

    并像这样更改您的 WelcomeScreen

    import React from 'react';
    import  { ImageBackround, StyleSheet, View, Image, Text} from 'react-native';
    import {Images} from './assets';
    
    function WelcomeScreen(props){
        return(
            <>
            <ImageBackround
            style={styles.background}
            source={Images.icon}
            >
            <View style={styles.logoContainer}>
    
            <Image style={styles.logo} source={Images.favicon}/>
            <Text> Sell what you dont need!</Text>
            </View>
            <View style ={styles.registerButton}></View>
            < View style= {styles.loginButton}></View>
            </ImageBackround>
            </>
        )
    }
    const styles = StyleSheet.create({
        background: {
            flex:1,
            alignItems: "center" ,
        
        },
        loginButton: {
            width: "100%",
            height: 70,
            backgroundColor: "#4ecdc4",
    
        },
        logo: {
            width: 100,
            height: 100,
    
        },
        logoContainer: {
            position: "absolute",
            top: 70,
            alignItems:"center",
        
        },
        registerButton: {
            width: "100%",
            height: 70,
            backgroundColor: "#4ecdc4"
        },
    });
    
    
    
    export default WelcomeScreen;
    

    【讨论】:

    • 我这样做了,但是现在它卡在启动画面(白色空白屏幕)上。
    • 你能给我看看你的导航文件和根屏幕吗
    • 我重新启动了我的项目,但仍然遇到同样的错误。所以,我不再有黑屏问题了。我在哪里可以找到导航文件和根屏幕?感谢您的快速响应,不胜感激
    • VS 代码的错误。它说:此错误位于:在WelcomeScreen(由App创建)中在App(由ExpoRoot创建)中在RCTView中的ExpoRoot(由View创建)在RCTView中的DevAppContainer(由AppContainer创建)中的View(由AppContainer创建)中(由 View 创建)在 View 中(由 AppContainer 创建)在 AppContainer 中。
    • 你只有欢迎屏幕?没有导航设置?
    猜你喜欢
    • 2021-11-11
    • 2021-03-25
    • 2021-08-03
    • 2020-01-31
    • 2018-10-02
    • 2020-05-09
    • 2021-06-27
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多