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行,這應該有助於