0

我一直试图让我的头在React Native中,因为我最近决定从Cordova切换到它。试图编辑“React Native init”文件结构

我一直在试图理解容器和组件文件应如何正确构造内部src以正确构建。

为此,我一直试图运行一个新文件“app.js”中的初始index.android.js代码,该文件是我在一个名为js的文件夹中创建的,该文件夹位于原始/ src/main文件夹中。

这是索引文件的代码

/*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); 

而且app.js文件可以在这个gist here找到。

我一直则接收到以下错误: Error

任何帮助将超过赞赏。 在此先感谢, 杰克。

回答

1

的阵营原生应用中的典型的设置是这样的:

└── 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.jsindex.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/componentssrc/containers - 完全取决于您!

+0

感谢您的详细答案@jevakallio。 强烈的帮助。 –

0

您的文件正在尝试导出线路48上的App,该线路不存在。你已经有第10行的export default class LearnD,所以省略48行,这应该有助于