Want to redirect page with Javascript in React-Router? Context is the best option, however official documentation tells that you can also use withRouter to put router prop to your component that would correctly perform history state transition:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import { withRouter } from 'react-router'; class PageLogin extends React.Component { login() { this.props.history.push('/some/location'); // for [email protected] it would be this.props.router.push('/some/location'); } render() { return ( <div className="login-page"> <form action=""> <div className="form-group"> <label>Username:</label> <input type="text"/> </div> <div className="form-group"> <label>Password:</label> <input type="text"/> </div> <div> <button onClick={this.login}>Login</button> </div> </form> </div> ) } } export default withRouter(PageLogin); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.