我有一个用React + Redux构建的应用程序。我正在使用React Router和React Router Redux进行路由。我注意到一些非常奇怪的行为。当你点击后退按钮一次,从0到-1,它是有效的。但是当您再次单击它时,您打算从-1转到-2,而不是返回到0。随后的单击在0和-1之间循环。这种行为是一致的,而不是从历史推送到最后一个位置。这是这些库的预期行为吗?如果不是,我是不是用错了?
以下是我认为的相关代码片段。这些文件并不完整(太大了),但我认为这是必要的,如果需要的话,愿意分享更多。
root.tsx
import { Router, browserHistory } from 'react-router'
import { syncHistoryWithStore } from 'react-router-redux'
const history = syncHistoryWithStore(browserHistory, store)
const renderAsyncConnect = props =>
<ReduxAsyncConnect
{...props}
helpers={{ client }}
filter={notDeferredItems}
/>
const Root = () =>
<Provider store={store} key="provider">
<Router render={renderAsyncConnect} history={history}>
{routes}
</Router>
</Provider>
export default Root
我还有一个中间件监听推送操作。我知道有人建议我使用history.listen
,但是这个中间件对竞争条件是健壮的,并且在我们过渡系统时只是为了兼容。
以下是路由中间件之后的中间件的净效果:
const browseStateMiddleware = ({ dispatch, getState }) => next => action => {
if (action.type === '@@router/LOCATION_CHANGE') {
// some dispatching of actions deleted here for clarity
return next(action)
}
}
create-store.js
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension/logOnly'
import { createStore as _createStore, applyMiddleware } from 'redux'
import { routerMiddleware } from 'react-router-redux'
import clientMiddleware from './middleware/client-middleware'
import browseStateMiddleware from './middleware/browse-state-middleware'
import combinedReducers from './modules/reducer'
import types from './modules/account/types'
export default function createStore(history, client, data, persister) {
const storeData = data
// Sync dispatched route actions to the history
const reduxRouterMiddleware = routerMiddleware(history)
const middleware = [
browseStateMiddleware,
reduxRouterMiddleware,
clientMiddleware(client),
analyticsMiddleware,
thunk,
]
const composeEnhancers = composeWithDevTools(
{
// options like actionSanitizer, stateSanitizer
}
)
// See https://stackoverflow.com/questions/35622588/how-to-reset-the-state-of-a-redux-store/35641992#35641992
const rootReducer = (state, action) => {
if (action.type === types.LOGOUT) {
if (persister) {
persister.clear()
}
return combinedReducers(undefined, action)
}
return combinedReducers(state, action)
}
const store = _createStore(
rootReducer,
storeData,
composeEnhancers(
applyMiddleware(...middleware)
// other store enhancers
)
)
window.store = store
if (__DEVELOPMENT__ && module.hot) {
module.hot.accept('./modules/reducer', () => {
store.replaceReducer(System.import('./modules/reducer'))
})
}
return store
}
这里有什么问题吗?
发布于 2018-07-24 04:15:05
此问题最有可能是由以下原因引起的:
const browseStateMiddleware = ({ dispatch, getState }) => next => action => {
if (action.type === '@@router/LOCATION_CHANGE') {
// some dispatching of actions deleted here for clarity
return next(action)
}
}
您发送的操作发生在存储中的路由更新之前,这会导致此行为源自react-router-redux (我在4.0.8版中看到过它的重现)。
处理它的一种方法可能是执行以下操作:
const browseStateMiddleware = ({ dispatch, getState }) => next => action => {
if (action.type === '@@router/LOCATION_CHANGE') {
next(action);
// dispatch your actions here
return;
}
}
这样,您就可以保证在正确的时间正确地更新商店。
https://stackoverflow.com/questions/46714728
复制相似问题