我收到并执行基本反应导航时出错。启动应用程序时出现此错误,当我注释掉'const {navigate} = this.props.navigation;'行时''没有错误。反应导航错误的原因是什么,'this.props.navigation'未定义
App.js代码
import React from 'react';
import {AppRegistry, StyleSheet, Text, View, TextInput, TouchableOpacity,
Button, KeyboardAvoidingView, ToastAndroid} from 'react-native';
import { StackNavigator,} from 'react-navigation';
import Home from './myviews/Home';
export default class Myapp extends React.Component {
static navigationOptions = {
title: 'Welcome',
};
constructor (props)
{
super(props);
this.state = {
text : 'Hi there...'
,username : ''
,password : ''}
}
login =() => {
}
render() {
const { navigate } = this.props.navigation;
let text = this.state.text;
return (
<KeyboardAvoidingView behavior="padding" style = {styles.container}>
<View style = {styles.child1}>
</View>
<View style = {styles.child2}>
<Text>{text} </Text>
<TextInput onChangeText = {(value)=>this.setState({username:value})} style = {styles.txtBig} placeholder = "username or email"></TextInput>
<TextInput onChangeText = {(value)=>this.setState({password:value})} style = {styles.txtBig} placeholder = "password" ></TextInput>
<TouchableOpacity style = {styles.button} onPress={()=>{ this.login() }}>
<Text style={styles.txt}>Login</Text>
</TouchableOpacity>
<Button
onPress={() => navigate('Home')}
title="Go home"
/>
</View>
</KeyboardAvoidingView>
);
}
}
const App = StackNavigator({
Myapp: { screen: Myapp },
Home: { screen: Home },
});
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: '#7fffd4',
},
child1 : {
flex: 1,
flexDirection: 'row',
justifyContent : 'space-around',
alignItems : 'center',
backgroundColor: '#00ffff',
},
child2 : {
flex: 1,
alignItems : 'center',
backgroundColor: '#ff8c00',
},
grandchild1 : {
width:150,
height:50,
backgroundColor : '#ff8c00',
},
grandchild2 : {
width:150,
height:50,
backgroundColor : '#8fbc8f'
},
txtBig : {
width : 300,
marginTop : 10,
backgroundColor : 'white',
borderWidth: 3,
borderColor : 'white',
paddingHorizontal : 10,
fontSize : 20,
color : '#ff8c00',
height : 50,
},
txt : {
textAlign : 'center',
fontSize : 20,
color : '#ff8c00',
fontWeight : '700'
},
button : {
backgroundColor : '#ffd700',
width : 300,
height : 50,
paddingHorizontal : 10,
paddingVertical : 10,
marginTop : 10,
}
});
AppRegistry.registerComponent('Myapp',() => App);
在我渲染()如果注释掉“this.props.navigation”没有错误 ,我没有看到在“欢迎”称号对myApp屏幕要么
Home.js代码
import React from 'react';
import {AppRegistry, StyleSheet, Text, View, TextInput, TouchableOpacity,
KeyboardAvoidingView, ToastAndroid} from 'react-native';
export default class Home extends React.Component {
constructor (props)
{
super(props);
}
render() {
return (
<KeyboardAvoidingView behavior="padding" style = {styles.container}>
<View style = {styles.child1}>
<Text>Welcome Home</Text>
</View>
</KeyboardAvoidingView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: '#7fffd4',
},
child1 : {
flex: 1,
flexDirection: 'row',
justifyContent : 'space-around',
alignItems : 'center',
backgroundColor: '#00ffff',
},
});
AppRegistry.registerComponent('Home',() => Home);
显示有效的代码,然后我们可以提供帮助。您的App.js代码根本无效。 – hyb175
你在App.js中有语法错误 –
嗨,谢谢你的回复。我已经包括了App.js和Home.js的所有代码 –