我一直试图绕过React Native,因为我最近决定从Cordova切换到它。
我一直在努力了解如何在src中正确构建容器和组件文件以便正确构建。
为此,我一直试图从一个新文件" app.js"中运行初始的index.android.js代码。我在原始/ src / main文件夹中找到的名为js的文件夹中创建了它。
这是索引文件代码
/*Both index files index.ios.js and index.android.js MUST be indentical*/
var React= require('react-native');
var { AppRegistry } = React;
var App = require('./android/app/src/main/js/app.js')
AppRegistry.registerComponent('LearnD', () => LearnD);
可以在此gist here找到app.js文件。
任何帮助都将得到更多的赞赏。 提前致谢, 杰克。
答案 0 :(得分:1)
React Native应用程序的典型设置如下所示:
└── YourApp
├── android # Native Android files, no .js here
├── index.android.js # Android entry point, must exist
├── index.ios.js # iOS entry point, must exist
├── ios # Native iOS files, no .js here
└── src # All your .js sources
├── components # All your .js sources
└── main.js # Your shared entry point
您的src/main.js
可以为两个平台导出单个共享入口点组件,并使用src/
目录中的其他组件:
// src/main.js
import React, { Component } from 'react';
import { View } from 'react-native';
import OtherComponent from './components/other-component.js'
// note export default
export default class Main extends Component {
render() {
return (
<View>
<OtherComponent />
</View>
);
}
}
您的index.ios.js
和index.android.js
组件可以导入主要组件并将其注册为应用程序根组件:
// index.ios.js and index.android.js
// both must exist, but they can be identical
import { AppRegistry } from 'react-native';
import Main from './src/main';
AppRegistry.registerComponent('YourApp', () => Main);
在src
目录中,您可以以您认为合适的任何方式构建应用代码,例如src/components
和src/containers
- 完全取决于您!
答案 1 :(得分:0)
您的文件正在尝试在第48行导出App
,该行不存在。你已经在第10行有export default class LearnD
,所以省略第48行,这应该有帮助