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=>{

                })
        }
    }
0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.