react-router-redux:路由更改但不显示组件

时间:2016-12-23 19:38:58

标签: javascript reactjs redux react-router react-router-redux

我尝试使用react-router-redux和Immutable.js reducer建立路由。

我已使用syncHistoryWithStore设置商店,当我点击Link时,我可以看到发送了正确的@@router/LOCATION_CHANGE操作,商店是使用位置信息正确更新到基本reducer的routing键,并且URL正在更改为正确的位置。

问题是组件不会更新。如果我在特定位置加载页面,我会看到正确的组件,但在此之后单击Link后,不会显示其他组件。

我已经包含了我认为的相关文件(包含一些不相关的信息):

initialize.js:

import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly'
import { browserHistory } from 'react-router'
import { routerMiddleware, syncHistoryWithStore } from 'react-router-redux'
import reducers from 'reducers'

const browserHistoryRouterMiddleware = routerMiddleware(browserHistory)

const middlewares = [browserHistoryRouterMiddleware]

/* Create store */
const store = createStore(
  reducers,
  undefined,
  composeWithDevTools(
    applyMiddleware(...middlewares),
  ),
)

/* Configure history */
const createSelectLocationState = () => {
  let prevRoutingState, prevRoutingStateJS

  return (state) => {
    const routingState = state.get('routing')
    if (typeof prevRoutingState === 'undefined' || prevRoutingState !== routingState) {
      prevRoutingState = routingState
      prevRoutingStateJS = routingState.toJS()
    }
    return prevRoutingState
  }
}

const history = syncHistoryWithStore(browserHistory, store, {
  selectLocationState: createSelectLocationState(),
})

export { store, history }

index.js:

import React from 'react'
import { render } from 'react-dom'
import { store, history } from 'initialize'
import Root from 'components/Root'

render(
  <Root store={store} history={history} />,
  document.getElementById('root')
)

Root.js:

import React, { PropTypes } from 'react'
import { Provider } from 'react-redux'
import { Router } from 'react-router'
import routes from 'routes'

const Root = ({ store, history }) => (
  <Provider store={store}>
    <Router history={history}>
      {routes}
    </Router>
  </Provider>
)

Root.propTypes = {
  store: PropTypes.object.isRequired,
}

export default Root

routes只是RouteIndexRoute组件的嵌套列表,从<Route path='/' component={App} />开始。当我console.log应用程序的孩子支持时,我可以看到有儿童在第一次渲染时通过并正确呈现它们,但在其他任何时候我都看不到有关儿童更改的控制台反馈。 / p>

记录操作(状态是不可变地图,但我运行toJS()用于记录目的): Logged Action

我错过了哪些内容应该在我浏览路线时更改显示的组件?

1 个答案:

答案 0 :(得分:0)

问题在于我从prevRoutingState而不是createSelectLocationHistory返回prevRoutingStateJS