2016-09-08 16 views
2

基本的に私はいくつかのjsファイルに反応ネイティブコードを分割したいと思います。私は私の主な 'index.ios.js'ファイルを持っています。私は別のファイルにいくつかのコード(MainComponentクラス)に移動:「./iOSCode/main.js'ネイティブ - ファイル全体/複数のファイルに分割jsコードをインポート

私は」インデックスに全体の 『./iOSCode/main.js』ファイルのコンテンツをインポートするにはどうすればよいです.ios.js '?

私が試した:運と

import { MainComponent as MainComponent } from './iOSCode/main.js'; 

and 

import MainComponent from './iOSCode/main.js'; 

を...

を私はエラーを取得:

Element type is invalid: expected a string (for built-in components) or class/function [...] but got: undefined 
intantiateReactComponent.js:70 
instantiateChild ReactChildReconciler.js:45 

==========

編集:

@ Jagadish Upadhyay提案私は内部のMainComponentクラス近く輸出文が欠落していた」./iOSCode/main.js'

コード:

import React, { Component } from 'react'; 
import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
    ListView 
} from 'react-native'; 

export class MainComponent extends Component { 

    render() { 
    return (
     <Text> abc </Text> 
    ); 
    } 
} 
+1

あなたはクラスをエクスポートしましたか?あなたはコードを投稿できますか? –

+0

はい、私はMainComponentクラスの 'export'ステートメントがありませんでした。ありがとう。 まだファイル全体をインポートする方法はありますか? 「./iOSCode/main.js」の1つのクラスだけでなく、 –

+1

@ kosiara-BartoszKosarzycki 2つのクラスをエクスポートすると、すべてのエクスポートをインポートする方法があります。たとえば、2つのクラスをエクスポートすると、 'main *からNameOfModuleとして' import *を入力でき、 'NameOfModule'にはすべての輸出 – azium

答えて

4

はあなたが輸出ステートメントを追加する必要があるクラスを分離し、

import { MainComponent as MainComponent } from './iOSCode/main.js'; 

スタイルを別のファイルに移動することもできます。

styles.js:

import { 
    StyleSheet 
} from 'react-native'; 

const styles = StyleSheet.create({ 

    welcome: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    }, 
}); 

module.exports = styles; 

とメインのjsファイルにこのインポートを使用します。

import * as styles from './styles.js'; 

を追加することを忘れないでください: 'module.exportsは=スタイルを;'

関連する問題