【问题标题】:React.createElement: type is invalidReact.createElement:类型无效
【发布时间】:2017-12-22 11:47:45
【问题描述】:

我是新来的反应和反应本机。我想使用一个库来反应原生。 https://github.com/FaridSafi/react-native-gifted-chat 但是我收到了这个错误:

警告:React.createElement:类型无效——需要一个字符串 (对于内置组件)或类/函数(对于复合 组件)但得到:对象。您可能忘记导出您的 来自定义它的文件中的组件。

在 registerRootComponent.js:21 检查您的代码。 在 ExponentRootComponent (在 renderApplication.js:35) 在 RCTView (在 View.js:128) 在视图中(在 AppContainer.js:93) 在 RCTView (在 View.js:128) 在视图中(在 AppContainer.js:92) 在 AppContainer 中(在 renderApplication.js:34 处)

这是我的代码:

import React from 'react';
import { StyleSheet, Text, View, KeyboardAvoidingView, Image, TextInput } from 'react-native';
import GiftedChat from 'react-native-gifted-chat';

class App extends React.Component {

  state = {
    messages: [],
  };


  componentWillMount() {
    this.setState({
      messages: [
        {
          _id: 1,
          text: 'Hello developer',
          createdAt: new Date(),
          user: {
            _id: 2,
            name: 'React Native',
            avatar: 'https://facebook.github.io/react/img/logo_og.png',
          },
        },
      ],
    });
  }

  onSend(messages = []) {
    this.setState((previousState) => ({
      messages: GiftedChat.append(previousState.messages, messages),
    }));
  }

  render() {
    return (
      <GiftedChat
        messages={this.state.messages}
        onSend={(messages) => this.onSend(messages)}
        user={{
          _id: 1,
        }}
      />
    );
  }

}

我添加这个库:

yarn add react-native-gifted-chat

我使用 Expo-XDE 在 android 模拟器上启动我的应用程序。

【问题讨论】:

标签: reactjs react-native


【解决方案1】:

导出App 组件进行渲染。

【讨论】:

  • 对不起,它工作了一秒钟然后又崩溃了......我添加了这个“导出默认应用程序;”在顶部
  • AppContainer.js:93,这行代码是什么?
【解决方案2】:

如警告中所述,您可能忘记从定义组件的文件中导出组件。

只需在文件底部添加导出

App extends React.Component{ ... }
export default App

【讨论】:

    猜你喜欢
    • 2020-04-25
    • 2018-07-22
    • 2019-08-19
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2017-07-17
    • 2019-11-03
    相关资源
    最近更新 更多