0

I have a singinUser function which is called when user clicks the submit button. I want the user to redirect to another url when fetching the data is completed. In previous versions of react, I used to use browserHistory from 'react-router'. What should I do in react-router 4?

import axios from 'axios';
    const ROOT_URL = 'http://localhost:3090';

    export const signinUser = ({ email, password }) => {
        return (dispatch) => {
            axios.post(`${ROOT_URL}/signin`, { email, password })
                .then(response =>{
                    //I want to redirect
                })
                .catch(err=>{

                })
        }
    }
Unity Hour
  • 498
  • 6
  • 19

1 Answers1

0

This is personally how I perform a redirect for protected routes in my application:

const GuestRoute = ({ component: Component, ...rest }) => (
  <WebServiceContextConsumer>
    {
      context =>
        <Route
          {...rest}
          render={props =>
            context.auth.getUser() 
              ? (<Redirect to={{pathname: "/", state: { from: props.location }}} />) 
              : (<Component {...props} />)
          }
        />
    }
  </WebServiceContextConsumer>
);

You need to separate your view from your logic. Perform your whatever call, then uses promises to catch the response and handle it however you want in your View component.

GiamPy
  • 3,240
  • 1
  • 25
  • 45