I'm getting error "Cannot read property 'props' of undefined" in ReactJS. I want to route the page to another page after login and also pass the user token for the session till logout for my app. My code :
...
import { withRouter } from 'react-router';
import { EventList } from "../EventList/EventList";
export class SignIn extends React.Component {
constructor(props) {
super(props);
this.state = {
fields: {
username: '',
password: ''
},
errors: {}
}
this.onSubmit = this.onSubmit.bind(this);
}
...
onSubmit(e){
e.preventDefault();
if(this.handleValidation()){
var apiBaseUrl = "http://api.eventsacross-stage.railsfactory.com/api/";
var input = this.state.fields;
axios.post(apiBaseUrl+'v1/users/signin', input)
.then(function (response) {
console.log(response);
if(response.data.status == 200){
console.log("Login successful");
alert("Login successful");
this.props.router.push('/EventList');
}
else if(...
}
})...
});
}
}
...
render() {
return (
...
<button type="submit" className="btn btn-primary btn-lg pull-right" onClick={this.onSubmit}>Sign In</button>
...
);
}
}
export default withRouter(SignIn);