首页 > 解决方案 > 反应本机错误 - 期望一个字符串或一个类但得到对象

问题描述

我是反应原生的新手。我试图编写从教程中找到的简单程序,以熟悉 react-native 环境。我研究了 stackoverflow 本身的相同错误,但找不到任何有用的解决方案,这就是我再次写信的原因。

我指的是以下教程

https://levelup.gitconnected.com/getting-started-with-react-native-in-2019-build-your-first-app-a41ebc0617e2

以下是我使用的代码。创建了一个名为 EmojiDict.js 的新类

import React, { Component } from 'react'
import { View, Text, StyleSheet } from 'react-native'

class EmojiDict extends Component {
state = {
    '': ' Smiley',
    '': ' Rocket',
    '⚛️': '⚛️ Atom Symbol'
};

render() {
    return (
        <View style={styles.container} >
            <Text>this.state[''] </Text>
        </View>
    );
  }
}

const styles = StyleSheet.create({
container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF'
},
instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5
  }
});

然后修改 App.js 类如下:

import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View } from 'react-native';
import EmojiDict from './src/components/EmojiDict';

export default class App extends Component {
render() {
    return <EmojiDict />;
  }
}

我尝试了各种示例,但在 App.js 类被修改时没有任何效果。由于此错误,我无法继续前进。

标签: react-native

解决方案


看起来你忘记导出EmojiDict组件了

代替class EmojiDict extends Component {

尝试

export default class EmojiDict extends Component {

可以解决这个问题


推荐阅读