2016-09-08 5 views
2

은 기본적으로 내가 여러 JS 파일에 반응 네이티브 코드를 분할 할 여러 파일로 전체 파일/분할 JS 코드를 가져옵니다. 나는 나의 주 'index.ios.js'파일이 있습니다. 나는 다른 파일에 일부 코드 (MainComponent 클래스) 이동 : './iOSCode/main.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 클래스의 '내보내기'문이 누락되었습니다. 감사. 전체 파일을 가져 오는 방법이 있습니까? './iOSCode/main.js'의 한 클래스가 아닌가 ?? –

+1

@ kosiara-BartoszKosarzycki 당신이 의미하는 것이면 모든 수출품을 수입 할 수있는 방법이 있습니다 ... 예를 들어 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 = 스타일;'

관련 문제