2017-04-14 89 views
1

我試圖創建一個使用原生做出反應,像這樣的組件:陣營本地無狀態組件說不確定

export class IndicatorOverlay extends Component { 
    render() { 
    return (
     <View> 
     <Text>text</Text> 
     </View> 
    ); 
    } 
}; 

以上的作品,但是當我儘量做到無狀態是這樣的...

export default ({ text = 'text' }) => { 
    return (
    <View> 
     <Text>{text}</Text> 
    </View> 
); 
}; 

我得到以下錯誤:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.

我敢肯定,我失去了一些基本的東西,但我無法看到它。我在React網絡應用中使用了一個類似的無狀態組件,並沒有問題。

使用反應16.0.0-alpha.6和反應原生0.43.2,並在iPhone模擬器中看到此錯誤。

希望有人能幫助:)

+0

我遇到類似的這種行爲。但試試'const IndicatorOverlay =(...)=> {...}'然後'export default IndicatorOverlay'。它應該工作。 – zvona

回答

3

這可能是因爲第一個例子是一個叫出口,而第二個是默認的,因此一個方式需要導入它們是不同的。

假設您導入模塊,像這樣:

import { IndicatorOverlay } from 'IndicatorOverlay'; 

你有兩個選擇。或者:

1)改變您導入模塊(因爲無狀態組件的方式是默認的導出現在):

import IndicatorOverlay from 'IndicatorOverlay'; 

2)保持進口不變,但重構無狀態組件是這樣的:

export const IndicatorOverlay = ({text = 'text'}) => { 
    return (
    <View> 
     <Text>{text}</Text> 
    </View> 
); 
}; 

你可以使它更幹BTW:

export const IndicatorOverlay = ({ text = 'text' }) => (
    <View> 
    <Text>{text}</Text> 
    </View> 
); 

您可以閱讀米關於MDN進口和出口礦石:

+0

太神奇了!謝謝。和+1讓它更幹一點... :) – Damien