I am using react router 4 not react router 3 so I am using react-router-dom. I am trying to get this.props.history.push to work, but all it is doing is keeping the view the same, but the URL changes.
For example I click the button and it calls a function which just calls this.props.history.push('/posts'). The url changes from localhost:3000/signin to localhost:3000/posts. But I am still on my signin view.
If I hit refresh however it will redirect to the posts page. I have looked at other questions and those people could not get it to go there no matter what, even if they refreshed or went into the url and hit enter.
I will post the code below to see what I am doing wrong. Also I am using react router v4 not 3. I cannot use browserHistory.push and other things that v3 used.
Code:
component:
import React, { Component } from 'react';
import {Field, reduxForm} from 'redux-form';
import {connect} from 'react-redux';
import * as actions from '../../actions';
import {withRouter} from 'react-router';
class Signin extends Component {
constructor(props) {
super(props);
this.onSubmit = this.onSubmit.bind(this);
}
renderField(field) {
const { meta: {touched, error} } = field;
const className = `form-group ${touched && error ? 'has-danger' : ''}`;
return (
<div className={className}>
<label><strong>{field.label}:</strong></label>
<input
className="form-control"
type={field.type}
{...field.input}
/>
<div className="text-help">
{ touched ? error : ''}
</div>
</div>
)
}
onSubmit({email, password}) {
// this.props.signinUser({email, password}, () => {
this.props.history.push('/posts');
// });
}
renderAlert() {
if(this.props.errorMessage) {
return (
<div className="alert alert-danger">
<strong>Oops</strong> {this.props.errorMessage}
</div>
)
}
}
render() {
const { handleSubmit } = this.props;
return (
<form onSubmit={handleSubmit(this.onSubmit)}>
<Field
label="Email"
name="email"
type="email"
component={this.renderField}
/>
<Field
label="Password"
name="password"
type="password"
component={this.renderField}
/>
{this.renderAlert()}
<button type="submit" className="btn btn-success">Sign In</button>
</form>
);
}
}
function validate(values) {
const errors = {};
//Validate the inputs from values
if(!values.email) {
errors.email = "Enter an email!";
}
if(!values.password) {
errors.password = "Enter a password!";
}
//If errors is empty, the form is fine to submit
//If errors has any properties, redux form assumes form is invalid
return errors;
}
function mapStateToProps(state) {
return {
errorMessage: state.auth.error
};
}
export default reduxForm({
validate,
form: 'signin'
})(connect(mapStateToProps, actions)(withRouter(Signin)));
action:
export function signinUser({email, password}, cb) {
return function(dispatch) {
axios.post(`${ROOT_URL}/signin`, {email, password})
.then((response) => {
dispatch({type: AUTH_USER});
localStorage.setItem('token', response.data.token);
})
.then(() => cb())
.catch((error) => {
dispatch(authError(error.response.data.error));
})
}
}