【问题标题】:Android emulator error--Expected a component class got [object,object]Android emulator error--Expected a component class got [object,object]
【发布时间】:2017-09-28 04:53:25
【问题描述】:

即使代码看起来是准确的,React-native 也会抛出这个奇怪的异常。我正在使用 react-native 0.48。

附上index.js和login.js源码

Login.js

import React, { Component } from "react";
import {
  AppRegistry,
  Text,
  View,
  TouchableOpacity,
  Label,
  TextInput
} from "react-native";
import { StackNavigator } from "react-navigation";
class LoginPage extends Component {
  constructor(props) {
    super(props);
  }

  render(){
   // const { navigate } = this.props.navigation;
    return(
      <View>
        <Label Text="Login" />
        <TextInput placeholder="UserName" />
        <TextInput placeholder="Password" />
        <TouchableOpacity Text="Login" />
      </View>
    );
  }
}

export default LoginPage;

Index.js

//Root Component

import React, { Component } from "react";
import { AppRegistry, View, Text } from "react-native";
import Root from "./config/router.js";
import LoginPage from './components/user/Login';
 class App extends React.Component {
  render() {
    return (

      <View>
        <LoginPage />
        <Text>this is index component </Text>
      </View>
    );
  }
}

export default App;

即使我也看到了与此问题相关的 stackoverflow 问题 StackOverflow .. 我仍然无法找出问题所在。

路由器.js

//Screen router
import React, { Component } from "react";
import { StackNavigator } from "react-navigation";
import LoginPage from "../components/user/Login";
import Register from "../components/user/Register";
//import App from "../index";

export const Root = StackNavigator({
  login: { screen: LoginPage },
  Register:{screen:Register}
});

【问题讨论】:

    标签: reactjs react-native ecmascript-6


    【解决方案1】:

    如果你正在导入或使用它,你不能将第一个字母用作你的组件的一个小字母。组件的第一个字母必须是大写,否则你会得到这个错误

        <label Text="Login" /> //invalid
    <Label Text="Login" />  //Valid
    

    【讨论】:

    • 现在它的渲染'期望一个字符串或类/函数,但未定义。 ' 也用 router.js 文件更新我的问题
    • 它因为 Label 不是 react-native 的一部分,如果你想使用 Label 然后使用 native-base,使用 native-base fire 命令“npm install native-base --save”然后从中导入标签
    • 从所有文件中删除标签。仍然显示此错误“应为字符串或类/函数,但未定义。您可能忘记从其定义的文件中导出组件。检查应用程序的渲染方法
    • 为避免混淆,将 Login.js 重命名为 LoginPage.js,并尽量不要更改代码中的任何其他内容
    • 已更改。还是同样的问题。 LoginPage 中的代码是否无错误
    猜你喜欢
    • 2022-08-17
    • 2021-04-29
    • 2021-03-30
    • 2013-09-20
    • 2021-05-13
    • 2015-10-19
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多