Search code examples
reactjsreact-router-redux

react-router-redux does not change content/view


I have the react-router-redux set up as this example. But dispatch(push(url)) does not change the content/view nor the url on address bar. Even though from my console I can see that the LOCATION_CHANGE and CALL_HISTORY_METHOD are successfully called with my given address. In this case, if the sign in is success, it does not load the expected redirect address.

a sign in action

export function loginUser(email, password, redirect="/dashboard") {
  return function(dispatch) {
    dispatch(loginUserRequest());
    return fetch(`http://localhost:3000/users/sign_in/`, {
      ...
    })
    .then(parseJSON)
    .then(response => {
      try {
        let decoded = jwtDecode(response.token);
        dispatch(loginUserSuccess(response.token));
        dispatch(push(redirect));
      } catch (e) {
        ...
      }
    })
  }
}

routes.js

import React from 'react';
import { Router, Route, browserHistory, IndexRoute } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux'
...
import store from '../store';

const history = syncHistoryWithStore(browserHistory, store)

let Routes = 
  <Router history={history}>
    <Route path='/' component={MainContainer}>
      <IndexRoute component={Home} />
      <Route path='/sign_in' component={SigninForm} />
      <Route path='dashboard' component={authenticateComponent(Dashboard)} />
    </Route>
  </Router>

export default Routes;

store.js

import { createStore, applyMiddleware, compose } from 'redux'
import thunkMiddleware from 'redux-thunk'
import createLogger from 'redux-logger'
import reducers from './reducers';

const createStoreWithMiddleware  = compose(
  applyMiddleware(
    thunkMiddleware,
    createLogger()
  )
)

const store = createStore(reducers, createStoreWithMiddleware);

export default store;

AuthenticateComponent.js

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { push } from 'react-router-redux';

export function authenticateComponent(Component) {

  class AuthenticateComponent extends Component {

    componentWillMount () {
      this.checkAuth(this.props.isAuthenticated);
    }

    componentWillReceiveProps (nextProps) {
      this.checkAuth(nextProps.isAuthenticated);
    }

    checkAuth (isAuthenticated) {
      if (!isAuthenticated) {
        let redirectAfterLogin = this.props.location.pathname;
        this.props.dispatch(push(`/sign_in?next=${redirectAfterLogin}`));
      }
    }

    render () {
      return (
        <div>
          {this.props.isAuthenticated === true
            ? <Component {...this.props}/>
            : null
          }
        </div>
      )
    }
  }

  const mapStateToProps = (state) => ({
    token: state.auth.token,
    isAuthenticated: state.auth.isAuthenticated
  });

  return connect(mapStateToProps)(AuthenticateComponent);

}

I have put the routing: routerReducer into my combined reducers as well. What could be the problem of this?


Solution

  • Turns out I need to apply the routerMiddleware as well