首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >react-router-redux不会更改内容/视图

react-router-redux不会更改内容/视图
EN

Stack Overflow用户
提问于 2016-09-04 14:13:49
回答 1查看 1.6K关注 0票数 0

我将react-router-redux设置为这个example。但是dispatch(push(url))不会改变内容/视图,也不会改变地址栏上的url。即使在我的控制台中,我也可以看到使用我给定的地址成功地调用了LOCATION_CHANGE和CALL_HISTORY_METHOD。在这种情况下,如果登录成功,则不会加载预期的redirect地址。

一个实际行动的标志

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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);

}

我已经把routing: routerReducer放进了我的组合式减速器。这会有什么问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-04 22:40:33

原来我也需要应用routerMiddleware

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39314052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档