【问题标题】:Index.js:Missing Initializer in const declarationIndex.js:const 声明中缺少初始化程序
【发布时间】:2023-05-19 00:14:01
【问题描述】:

我一直在尝试 react-native 开发,我正处于第一阶段,所以我试图创建一个简单的 Hello World 应用程序,但系统一直显示我无法解决的错误。

This is the result it shows.

我的 index.js 文件如下:

   import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);

主App.js文件如下

export default class App extends React.Component{
  render()
  return {(
    <View style={styles.container}>
    <Text style={styles.welcome}>
     Hello World!
   </Text>
    </View>
  );
}
};

我也修复了 babel 和 jest 问题,感谢任何帮助。 谢谢。

【问题讨论】:

标签: reactjs react-native jsx


【解决方案1】:

在渲染方法中,第一个大括号位置错误,应该在render()之后而不是return之后:

export default class App extends React.Component{
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Hello World!
        </Text>
      </View>
    );
  }
};

【讨论】:

  • 这似乎不是问题,我检查了教程并按照您所说的重写了代码,错误仍然存​​在。