我尝试使用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
只是Route
和IndexRoute
组件的嵌套列表,从<Route path='/' component={App} />
开始。当我console.log
应用程序的孩子支持时,我可以看到有儿童在第一次渲染时通过并正确呈现它们,但在其他任何时候我都看不到有关儿童更改的控制台反馈。 / p>
记录操作(状态是不可变地图,但我运行toJS()
用于记录目的):
我错过了哪些内容应该在我浏览路线时更改显示的组件?
答案 0 :(得分:0)
问题在于我从prevRoutingState
而不是createSelectLocationHistory
返回prevRoutingStateJS
。