[英]Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in ReactJS
[英]Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)
我正在尝试在我的本机应用程序中使用导航,但是收到错误。
这是我的index.js文件:
import React, { Component } from 'react'
import { AppRegistry } from 'react-native';
import App from './App';
class ChatNow extends Component {
render() {
return (
<App />
)
}
}
AppRegistry.registerComponent('ChatNow', () => App);
我收到以下错误:
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object
我还尝试注释掉该类,并按照创建基本index.js文件时的方式运行该类,但是遇到相同的错误。
在这种情况下,app.js文件旨在实现一个Navigator组件,该组件已从react native中弃用,但通过“ react-native-deprecated-custom-components”重新实现。
这是我的app.js代码:
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
TouchableOpacity
} from 'react-native';
import Navigator from 'react-native-deprecated-custom-components';
import MainScreen from './components/mainScreen'
import SignInScreen from './components/signInScreen';
export default class App extends Component {
_renderScene(route, navigator) {
switch(route.name) {
case 'SignInScreen':
return <SignInScreen />
case 'MainScreen':
return <MainScreen />
}
}
render() {
return(
<Navigator
initialRoute={{name: 'MainScreen', title: 'Welcome'}}
renderScene={this._renderScene}
style={styles.container}
sceneStyle={styles.sceneContainer}
/>
)
}
}
嗨,我相信问题是您没有将顶级组件传递给AppRegistry.registerComponent,而不是App应该是ChatNow,但是我个人使用它的方式(如果ChatNow没有做任何事情,您可以摆脱它)并执行以下操作:
import { AppRegistry } from 'react-native';
import App from './App';
AppRegistry.registerComponent('ChatNow', () => App);
我希望这会有所帮助,我尝试发表评论,但我没有足够的代表。
好吧,那是最愚蠢的事...
原来导致该问题的代码行是:
import Navigator from 'react-native-deprecated-custom-components';
应该是:
import { Navigator } from 'react-native-deprecated-custom-components';
哎呀。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.