1

So basically i am doing a post request for login and upon success i want to dispatch the response and redirect the user to profile page.

So dispatch the response works fine but upon doing dispatch(push('/profile')); the URL changes but the profile component doesn't get rendered.

if i simply do below code execution without the axios request it works fine and redirects.

export function logIn(values) {
   return (dispatch) => {
     dispatch({ type: LOGIN_USER, payload: {email: values.email} });
     dispatch(push('/profile'));
   };
 }

Actual code what i want to do. after dispatching the response which works fine and my redux store gets updated but the redirection doesn't work. it changes the url to /profile but the view doesn't changes

import axios from 'axios';
import { push } from 'connected-react-router';

const ROOT_URL = `${process.env.REACT_APP_SERVER_PATH}`;
export const LOGIN_USER = 'LOGIN_USER';

export function logIn(values) {
  const url = `${ROOT_URL}/login`;
  const request = axios({
    method: 'POST',
    url: url,
    data: values,
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
  });
  return (dispatch) => {
    request.then((response) => {
      dispatch({ type: LOGIN_USER, payload: response.data.user });
      dispatch(push('/profile'));
    })
    request.catch((error) => {
      // do something
    })
  };
}

ConfigStore:

import { createBrowserHistory } from 'history';
import { createLogger } from 'redux-logger';
import { applyMiddleware, compose, createStore } from 'redux'
import { routerMiddleware } from 'connected-react-router'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/es/storage'
import thunk from 'redux-thunk'
import createRootReducer from './reducers'

export const history = createBrowserHistory();

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const persistConfig = {
  key: 'root',
  storage: storage,
  whitelist: ['auth'] // can eention specific data root name

};

const reducers = persistReducer( persistConfig, createRootReducer(history));

export default () => {
  const store = createStore(
    reducers,
    composeEnhancers(applyMiddleware(routerMiddleware(history), thunk, createLogger()))
  );
  let persistor = persistStore(store)

  return  { store, persistor }
}
Nick Bb
  • 601
  • 1
  • 8
  • 18
  • Hi Nick. How have you setup your routing for this application. Are you using react-router-dom? – Chris Ngo Apr 24 '19 at 08:38
  • Yes, i am using react router. as mentioned if i don't do an ajax request then it redirects perfectly fine. That's why i am quite lost with it – Nick Bb Apr 24 '19 at 08:40
  • it works fine outside the response promise `request.then((response) => {}` works fine outside this... but inside it just changes the url and doesn't changes the component – Nick Bb Apr 24 '19 at 08:43
  • Hmmm I see this a solvable bug with connected-router. Based on the code you've provided above, I do not see any problem. But, this link here has a few solutions from people who have experienced your issue first-hand. Hopefully you can get an answer! https://github.com/supasate/connected-react-router/issues/159 – Chris Ngo Apr 24 '19 at 08:49
  • 1
    Have you used any middleware like redux-thunk. Please check this https://daveceddia.com/what-is-a-thunk/ – ravibagul91 Apr 24 '19 at 09:38
  • Yes, i am using redux-thunk. Do you mean that i should check request in middleware and redirect? – Nick Bb Apr 24 '19 at 09:40
  • @ravibagul91 the redirection is working fine outside the promise. and i am using redux-thunk. – Nick Bb Apr 24 '19 at 10:12
  • added my config-store code – Nick Bb Apr 24 '19 at 10:13
  • @ravibagul91 @yourfavoritedev downgrading `connected-react-router` to `6.0.0` helped and it works now. – Nick Bb Apr 24 '19 at 10:30

0 Answers0