我正在使用react-native-custom-navigation
开发一个本机应用程序但是,我收到一条显示undefined is not an object (evaluating 'this.props.route.push')
的错误消息。我不知道如何解决这个问题,以便我可以成功导航到新视图?
LoginWrapper.js
var React = require('react-native');
var Parse = require('parse/react-native').Parse;
var ParseReact = require('parse-react/react-native');
var AppWrapper = require('./AppWrapper');
var Routes = require('../Routes')
var {
TouchableHighlight,
Text,
TextInput,
View,
} = React;
var LoginWrapper = React.createClass({
mixins: [ParseReact.Mixin],
getInitialState: function() {
return {
error: null,
signup: false
};
},
observe: function() {
return {
user: ParseReact.currentUser
};
},
updateUsername: function(text) {
this.setState({username: text});
},
gotoProfile: function(){
this.props.route.push(Routes.Profile());
},
updatePassword: function(text) {
this.setState({password: text});
},
render: function() {
if (this.data.user) {
return (
<View style={{marginTop:100}}>
<TouchableHighlight onPress={this.logOut}>
<Text>Logout</Text>
</TouchableHighlight>
<TouchableHighlight onPress={this.gotoProfile}>
<Text>Profile</Text>
</TouchableHighlight>
<AppWrapper />
</View>
);
}
return (
<View>
<View>
{
this.state.error ?
<View><Text>{this.state.error}</Text></View> :
null
}
<View style={{marginTop:200}}>
<Text>Username</Text>
<TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1}} value={this.state.username} onChangeText={this.updateUsername} />
</View>
<View>
<Text>Password</Text>
<TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1}} value={this.state.password} onChangeText={this.updatePassword} />
</View>
<View>
<TouchableHighlight onPress={this.submit}>
<Text>{this.state.signup ? 'Sign up' : 'Log in'}</Text>
</TouchableHighlight>
</View>
<View>
<TouchableHighlight onPress={this.toggleSignup}>
<Text>{this.state.signup ? 'log in' : 'sign up'}</Text>
</TouchableHighlight>
</View>
</View>
</View>
);
},
submit: function() {
var self = this;
var username = this.state.username;
var password = this.state.password;
if (username.length && password.length) {
if (this.state.signup) {
console.log('signup');
var u = new Parse.User({
username: username,
password: password
});
u.signUp().then(function() {
self.setState({
error: null
});
}, function() {
self.setState({
error: 'Invalid account information'
});
});
} else {
Parse.User.logIn(username, password).then(function() {
self.setState({
error: null
});
}, function() {
self.setState({
error: 'Incorrect username or password'
});
});
}
} else {
this.setState({
error: 'Please enter all fields'
});
}
},
logOut: function() {
Parse.User.logOut();
},
toggleSignup: function() {
this.setState({
signup: !this.state.signup
});
}
});
module.exports = LoginWrapper;
Routes.js
class Routes {
static register(name, handler) {
if (this.handlers == null) this.handlers = {}
this.handlers[name] = handler
}
static get(name, params) {
if (this.handlers[name] == null) throw new Error('unknown route')
return this.handlers[name](params)
}
static Login() {
return {
component: require('./components/LoginScreen'),
title: 'Login',
titleStyle: {
color: '#ddd',
fontSize: 22
}
}
}
static Profile() {
return {
component: require('./components/ProfileScreen'),
title: 'Profile',
titleStyle: {
color: '#ddd',
fontSize: 22
}
}
}
}
module.exports = Routes