11

I using react-router in my application.

In my login page, I needing authentication with ajax and redirect if success.

Some like following code:

class PageLogin extends React.Component {
    login() {
        // How to can I redirect to another page if auth success?
    }

    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>
        )
    }
}

In my login function, how to can I redirect to another page if authentication success?

2 Answers 2

14

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:

import { withRouter } from 'react-router';

class PageLogin extends React.Component {
    login() {
        this.props.history.push('/some/location'); // for react-router@3 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);
Sign up to request clarification or add additional context in comments.

4 Comments

bravvoo..!! exactly what i was looking for Thanks a lot!!
link is 404, think new location is reacttraining.com/react-router/web/api/Router
for v.4.2.0 it is: this.props.history.push('/');
Doug & Konstantin, thanks, folks! The link and the reference property in the example are now updated.
1

You'll have a reference to the Router in context. You can simply call router.push with your new path to redirect.

class PageLogin extends React.Component {
  login() {
    this.context.router.push('/newPath');
  }
  ...
 }

PageLogin.contextTypes = {
  router: React.PropTypes.object
}

If you don't want to push a route to their history, but instead replace their current route, you can call replace instead. The API is identical to push.

1 Comment

This is not the current API of React Router.. the selected best answer is the correct usage now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.