2017-07-25 56 views
0

我創建一個JavaScript文件的SimpleButton一個自定義組件:自定義組件不確定的陣營本地

import React, { 
    Text, 
    View 
} from 'react-native'; 

export default class SimpleButton extends React.Component{ 
    render(){ 
    return (
     <View> 
     <Text>Simple Button</Text> 
     </View> 
    ); 
    } 
} 

然後我將它導入到指數的JavaScript文件:

/** 
* Sample React Native App 
* https://github.com/facebook/react-native 
* @flow 
*/ 

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

import SimpleButton from './App/Components/SimpleButton'; 

class ReactNotes extends React.Component { 
    render() { 
    return (
     <View style={styles.container}> 
     <SimpleButton/> 
     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    } 
}); 

AppRegistry.registerComponent('ReactNotes',() => ReactNotes); 

最後,在運行Android設備,它會拋出「超級表達式必須爲空或函數,而不是未定義的」錯誤。該錯誤導致SimpleButton類聲明代碼行('導出默認類SimpleButton擴展React.Component'。

我目前使用的是最新版本的React Native,我搜索了一些解決方案,但沒有成功。請幫助我!

回答

1

您正在使用import React from react-native,這是不實際的反應中,本地目前你應該import React from react本身 - 而不是我下面固定SimpleButton類

import { 
    Text, 
    View 
} from 'react-native'; 

import React from 'react'; 

export default class Test extends React.Component{ 
    render(){ 
    return (
     <View> 
     <Text>Simple Button</Text> 
     </View> 
    ); 
    } 
} 

你也可以只添加以下 - >import {Component} from 'react';,然後簡單地使用此功能作爲export default class Test extends Component{...

(注意這條線替換import React from 'react'解決我前面提到過)

希望這有助於

+0

應該由我添加的方式 –

+0

實現這兩個文件中的此修復程序「進口自陣營‘反應’; 「現在已經有效了,非常感謝! –