我在另一個文件夾中有一個名爲EnterName
的組件,我想在我的Navigator
中使用我的index.ios
文件。當我在同一個文件把EnterName
我沒有得到任何的問題,但是當我試圖從另一個文件導入它,我得到:如何在React Native中正確導入組件到導航器中?
Element type is invalid: expected a string (for built-in components
or a class/function (for composite components) but got: undefined.
Check the render method of `Navigator`
我已經試過進口EnterName成分的兩種不同的方式,既不工作:
import {EnterName} from './App/Components/EnterName'; var EnterName = require('./App/Components/EnterName');
下面是使用Navigator
,並試圖從另一個文件夾使用的組件EnterName
一些文本(當EnterName在同一個文件中聲明瞭這個工程)。
render() {
return (
<Navigator
initialRoute={{name: 'Name entry', index: 0}}
renderScene={(route, navigator) =>
<EnterName
name={route.name}
onForward={() => {
var nextIndex = route.index + 1;
navigator.push({
name: 'Scene ' + nextIndex,
index: nextIndex,
});
}}
onBack={() => {
if (route.index > 0) {
navigator.pop();
}
}}
/>
}
/>
);
}
}
而且,如果你想看到的EnterName文件,它在這裏:
import React, {
Text,
View,
Component,
Image,
StyleSheet
} from 'react-native';
class EnterName extends Component {
render() {
return (
<View style={styles.center}>
<View style={styles.flowRight}>
<TextInput style={styles.inputName}
placeholder="Enter your name"
textAlign="center"
onChangeText={(text) => this.setState({text})}
//value={this.state.text}
/>
<TouchableHighlight style={styles.button}
underlayColor='#99d9f4'>
<Text style={styles.buttonText}> Go </Text>
</TouchableHighlight>
</View>
</View>
)
}
}
// The stylesheet is here, and then below it I have:
module.export = EnterName;
你能不能幫我找出如何模塊化我的代碼?
編輯:我只是在module.exports結尾處忘了「s」。看起來像導出默認類_classname extends Component {是要走的路。
嘗試做'出口默認類EnterName擴展Component' –
我想,都與並且沒有'module.export = EnterName',然後使用我在index.ios類中導入的兩個變體。不工作。 – AlwaysQuestioning
你可以參考https://github.com/react-native-simple-router-community/react-native-simple-router –