这似乎是之前提出过的一个问题,但到目前为止我遇到的每个解决方案都是我在代码中已有的东西,或者解决了比我自己更复杂的代码库问题。我试图做的就是从React Native CLI生成的入门代码为我的应用程序创建一个中心入口点。这就是我到目前为止所拥有的:
index.ios.js:
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
import MyApp from './index';
AppRegistry.registerComponent('MyApp', () => MyApp);
index.android.js:
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
import MyApp from './index';
AppRegistry.registerComponent('MyApp', () => MyApp);
index.js:
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
export default class MyApp extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.ios.js
</Text>
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
</View>
);
}
}
然而,尝试运行该应用程序会产生以下结果:
以前的答案似乎建议使用
export default class
来解决问题,但在我创建应用程序时已经使用了。关于可能导致这种情况的任何建议?提前谢谢。
答案 0 :(得分:4)
尝试移出根文件夹的index.js。
创建一个文件夹&#34; src&#34;,将index.js移动到此文件夹并将其导入 index.ios.js 和 index.android.js 这样:
import MyApp from './src/index';
问题是,同一根路径下的index.js和index.ios.js并不是一个好主意。我刚试了一个测试应用程序。