【问题标题】:react-router-redux 5: Maintaining store state history across navigationreact-router-redux 5:跨导航维护存储状态历史
【发布时间】:2018-08-07 05:03:33
【问题描述】:

我目前正在开发一个 react redux 应用程序。当我在浏览器中使用上一个和下一个按钮导航时,url 正在更改并且路线得到正确呈现。但是,redux 存储保持最新状态并且不会跨导航进行时间旅行,唯一改变的状态是路由器位置状态。

这是我的 redux 商店:

import {applyMiddleware, combineReducers, createStore} from "redux";
import reducers from "../reducers";
import history from '../history'
import {routerMiddleware, routerReducer} from "react-router-redux";

const middleware = routerMiddleware(history)

const store = createStore(
  combineReducers({
    ...reducers,
    router: routerReducer
  }),
  applyMiddleware(middleware)
)

export default store

应用程序的索引文件是:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import 'semantic-ui-css/semantic.min.css';
import {registerObserver} from "react-perf-devtool";
import {Provider} from "react-redux";
import store from "./store";
import {ConnectedRouter} from "react-router-redux";
import history from './history'

if (module.hot) {
  module.hot.accept()
}

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <App />
    </ConnectedRouter>
  </Provider>,
  document.getElementById('root')
);
registerObserver();
registerServiceWorker()

历史如下:

import createHistory from 'history/createBrowserHistory'

const history = createHistory()

export default history

最后,这里是减速器:

import {SET_USER_LOCATION, SET_NEARBY_SHOPS} from "../constants/action-types";

const initialState = {
  userLocation: {
    latitude: 0.,
    longitude: 0.
  },
  nearbyShops: {
    shops: [],
    radiusOfSearch: 0.
  }
}

const userLocation = (state = initialState.userLocation, action) => {
  switch (action.type) {
    case SET_USER_LOCATION:
      return { latitude: action.payload.latitude, longitude: action.payload.longitude }
    default:
      return state
  }
}

const nearbyShops = (state = initialState.nearbyShops, action) => {
  switch (action.type) {
    case SET_NEARBY_SHOPS:
      return { shops: [...action.payload.shops], radiusOfSearch: action.payload.radiusOfSearch }
    default:
      return state
  }
}

const reducers = { userLocation, nearbyShops }

export default reducers

我使用推送导航到另一条路线:

const mapDispatchToProps = dispatch => bindActionCreators({
  setNearbyShops: nearbyShops => setNearbyShops(nearbyShops),
  goToNearbyShops: (latitude, longitude, radius) => push(`/nearby/@${latitude},${longitude},${radius}`)
}, dispatch)

我想要的是,当我在历史记录中来回导航时,userLocation 和 nearShops 状态会同步,因此组件会以正确的状态而不是最新状态呈现。

这是一个 gif,用于进一步解释我正在尝试解决的问题:

【问题讨论】:

  • 嘿,我也有同样的问题,发在这里github.com/ReactTraining/react-router/issues/5974如果你解决了,请评论,这不是包问题
  • 我一定会的!
  • 嘿,所以我想我有一个最终答案给你,这个功能不是 react-router-redux 的一部分。您/我们正在寻找类似 @​​987654323@ 的东西,或者只是编写我们自己的中间件来通过会话存储处理浏览器 url 与数据状态的同步
  • 或者一种方法是拦截浏览器的后退和下一步按钮,并以某种方式使其与 redux-undo 一起工作

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


【解决方案1】:

所以你想让你的 UI 直接依赖于历史状态。 不幸的是,这与 react-router 的现有 redux 包装器相当繁琐。

所以当历史改变时,你的App 组件会收到一个 prop 更新。 您必须以某种方式将这些新道具转发给应对它们做出反应的组件。 你可以例如在 componentWillReceiveProps of you App 组件的方法中将新信息发送到商店。

或者您可以通过多个组件层将道具转发到需要它们的位置,但我建议不要这样做。

或者,改用https://github.com/mksarge/redux-first-routing,这让恕我直言更有意义。 它使路由独立于组件。 另见这篇文章: https://medium.freecodecamp.org/an-introduction-to-the-redux-first-routing-model-98926ebf53cb

不管怎样,userLocationnearbyShops 不应该是缩减器,而是选择器,因为它们的信息可以而且应该从您历史中的地理位置中获得。

【讨论】:

  • 您能进一步解释一下吗?我看不出使用选择器如何解决我在导航中维护商店历史记录的问题。
  • 您说您的路由器位置状态确实已更新。您可以从商店中正确更新的状态中导出地理位置和附近的商店(可能是缓存的选择器)。我假设地理坐标位于浏览器位置?
  • 我已编辑帖子以显示 gif,以便更好地解释问题。当我按下后,路径会更新到以前的状态,但商店会保持其最后状态
  • 谢谢。我会尽量澄清我的答案。
猜你喜欢
  • 2019-07-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多