React Native报错Element type is invalid,求问题排查帮助
问题分析与解决方案
错误原因
你遇到的Uncaught Error: Invariant Violation: Element type is invalid错误,核心问题是HomeScreen中错误地从react包导入了Text和View组件。这两个组件属于react-native库,而非react库,当前导入的是整个react对象,并非可渲染的组件函数/类,导致React无法识别并渲染它们。
修复步骤
修改HomeScreen的导入语句,从react-native正确导入Text和View:
import React from "react"; // 替换错误的导入,从react-native导入组件 import { Text, View } from "react-native"; const HomeScreen = () => { return ( <View> <Text>Det er min home</Text> </View> ) }; export default HomeScreen
验证说明
修改后,Text和View会被正确识别为React Native的内置组件,此时导航到HomeScreen就能正常渲染内容,不会再抛出组件类型无效的错误。
内容的提问来源于stack exchange,提问作者Willnewbie
相关产品推荐
相关产品推荐

