I'm trying to redirect to user page after login. This is my code:
import React, {PropTypes} from 'react';
import styles from '../styles/login.scss';
import { connect } from 'react-redux';
import { checkLogin } from './../actions/login.action';
class Login extends React.Component {
constructor(props) {
super(props);
this.state = {
username: '',
password: ''
};
this.handleChangeUsername = this.handleChangeUsername.bind(this);
this.handleChangePassword = this.handleChangePassword.bind(this);
}
componentWillReceiveProps(nextProps) {
if (nextProps.wasSuccessful !== this.props.wasSuccessful) {
this.redirectToUserPage(nextProps.wasSuccessful);
}
}
redirectToUserPage = () => {
if (this.props.wasSuccessful) {
window.location.href = 'localhost:3000/filterableTable';
}
};
login =() => {
this.props.dispatch(checkLogin(this.state));
};
handleChangeUsername(event) {
this.setState({username: event.target.value});
}
handleChangePassword(event) {
this.setState({password: event.target.value});
}
render() {
console.log('----', this.props.wasSuccessful);
return (
<div className={styles.loginForm}>
<h3>Welcome</h3>
<div className={styles.container}>
<label><b>Username</b></label>
<input type="text" value={this.state.username} onChange={this.handleChangeUsername} name="username" placeholder="Username" required></input>
<label><b>Password</b></label>
<input type="password" value={this.state.password} onChange={this.handleChangePassword} name="password" placeholder="Password" required></input>
<input type="submit" value="Login" onClick={this.login}></input>
</div>
</div>
);
}
}
Login.propTypes = {
login: PropTypes.func.isRequired,
dispatch: PropTypes.func.isRequired,
wasSuccessful: PropTypes.boolean,
};
export default connect((state) => ({
wasSuccessful: state.login.wasSuccessful
}))(Login);
On wasSuccessful the state changess to True, so when i press login, i should be able to redirect to user page.
componentWillReceiveProps(nextProps) {
if (nextProps.wasSuccessful !== this.props.wasSuccessful) {
this.redirectToUserPage(nextProps.wasSuccessful);
}
}
I understood that this will be called when the state is changed, so in here i call the function redirectToUserPage.
redirectToUserPage = () => {
if (this.props.wasSuccessful) {
window.location.href = 'localhost:3000/filterableTable';
}
};
I tryed like this, but it doesn't work. Please, help~