简体   繁体   English

元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)

[英]Element type is invalid: expected a string (for built-in components) or a class/function (for composite components)

I am attempting to use navigation in my react native application, but am receiving an error. 我正在尝试在我的本机应用程序中使用导航,但是收到错误。

Here is my index.js file: 这是我的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);

I get the following error: 我收到以下错误:

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object

I also tried commenting out the class and just running it the way the base index.js file was when it was created, but I get the same error. 我还尝试注释掉该类,并按照创建基本index.js文件时的方式运行该类,但是遇到相同的错误。

In this case, the app.js file is designed to implement a Navigator component, deprecated from react native but reimplemented through 'react-native-deprecated-custom-components'. 在这种情况下,app.js文件旨在实现一个Navigator组件,该组件已从react native中弃用,但通过“ react-native-deprecated-custom-components”重新实现。

Here is my app.js code: 这是我的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}
      />
    )
  }
}

Hi I believe the problem is that you are not passing the top level component to AppRegistry.registerComponent, instead of App it should be ChatNow, but the way i've personally used it, if ChatNow is not doing anything you could just get rid of it and do this: 嗨,我相信问题是您没有将顶级组件传递给AppRegistry.registerComponent,而不是App应该是ChatNow,但是我个人使用它的方式(如果ChatNow没有做任何事情,您可以摆脱它)并执行以下操作:

import { AppRegistry } from 'react-native';
import App from './App';

AppRegistry.registerComponent('ChatNow', () => App);

I hope it helps, I tried just leaving a comment but i dont have enought rep. 我希望这会有所帮助,我尝试发表评论,但我没有足够的代表。

Okay, it was the dumbest thing... 好吧,那是最愚蠢的事...

Turns out the line of code that was causing the problem was: 原来导致该问题的代码行是:

import Navigator from 'react-native-deprecated-custom-components';

It should have been: 应该是:

import { Navigator } from 'react-native-deprecated-custom-components';

Oops. 哎呀。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:ReactJS 中的对象 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object in ReactJS 错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object NextJS:元素类型无效:期望字符串(用于内置组件)或类/函数(用于复合组件)但得到:未定义 - NextJS: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 元素类型无效:预期字符串(用于内置组件)或类/函数(用于复合组件)但得到:未定义 - Element type is invalid: expected string (for built-in components) or a class/function (for composite components) but got: undefined 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 渲染错误元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - Render Error Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined NextJS:错误 - 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:object - NextJS: error - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义 - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 控制台错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - Console error: Element type is invalid:expected a string (for built-in components) or a class/function (for composite components) but got: undefined ReactDOM - 元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - ReactDOM - Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM