简体   繁体   English

Redux:如何管理路由之间的冲突状态?

[英]Redux: How to manage conflicting state across routes?

In my application, on route1 I get a list of items from an api call. 在我的应用程序中,在route1上,我从api调用中获得了项目列表。 The component is connected and the items are saved in redux store and passed to the component as props. 连接了组件,并将项目保存在redux存储中,并作为道具传递给组件。 Now, on route2 I again need this list of items. 现在,在route2上,我再次需要此项目列表。 For now, i am fetching the list of items again when we move to route2 现在,当我们转到route2时,我将再次获取项目列表

  1. Does it make sense to reuse the list of items which is already in global state? 重用已经处于全局状态的项目列表是否有意义? If yes, how do we decide when it is stale and when to refetch? 如果是,我们如何确定何时过时以及何时重新提取?

  2. If it doesn't make sense to reuse the list of items, should i clear the list when moving away from route1/when loading route2? 如果重用项目列表没有意义,那么在加载route2时从route1 /移开时是否应该清除列表? Because otherwise, my component in route2 cannot start with an assumption that the list would be empty on load, and I would have to put an extra check for whether or not the list is empty. 因为否则,我在route2中的组件不能以列表在加载时为空的假设开始,因此我必须额外检查列表是否为空。 This would make my component kinda aware of the global state structure. 这会使我的组件有点了解全局状态结构。

  3. Does it make sense to name these pieces of state differently, so that one doesn't interfere with the other? 以不同的方式命名这些状态是否有意义,以使一个状态不会干扰另一个状态? This doesn't seem right, as both the functionalities are same, and we wouldn't want to increase the size of global state for this. 这似乎不对,因为两个功能都相同,并且我们不想为此增加全局状态的大小。

  4. is redux or any global state management library not a good choice for handling this kind of state? redux或任何全局状态管理库不是处理这种状态的好选择吗? This seems like state local to a route, which should not interfere with local state on another route, so it is not truly global. 这似乎是某个路由的本地状态,不应干扰其他路由的本地状态,因此它不是真正的全局状态。 Should we only keep global state in redux ? 我们应该只在redux中保持全局状态吗?

I have previously been using useReducer with axios calls to do this. 我以前一直在通过axios调用使用useReducer来做到这一点。 The state in that case was local to each of the components/route, so they didn't have to care about whether or not the state has been populated by someone else. 在这种情况下,状态是每个组件/路由的本地状态,因此他们不必担心状态是否已被其他人填充。 This approach had problems with sharing state between routes, but local state problem was handled well. 这种方法在路由之间共享状态方面存在问题,但是本地状态问题得到了很好的处理。

In this situation, I think you should store the item list in the global state as readonly data. 在这种情况下,我认为您应该以全局状态将项目列表存储为只读数据。 When a route component initialize, it should clone this item list from the global state and store it as local state. 路由组件初始化时,应从全局状态克隆此项目列表,并将其存储为本地状态。

This way ensures that the item list data is shared across multiple components without having to refetch it for every route while each component can choose to do whatever they want with its local data without affecting other components. 这种方式可以确保在多个组件之间共享项目列表数据,而不必为每个路径重新获取它,同时每个组件都可以选择使用其本地数据执行任何所需的操作而不会影响其他组件。 Also, when the component unmounts, it will clean up its local state so you don't have to worry about memory leak due to multiple copies of the item list. 另外,在卸载组件时,它将清理其本地状态,因此您不必担心由于项目列表的多个副本而导致的内存泄漏。

I have been in this situation and the approach I used was as follows: 我一直处于这种情况,我使用的方法如下:

  1. Keep one state for both routes. 两条路由都保持一个状态。 Since that's the only point of keeping a single source of truth across the application. 因为那是在整个应用程序中保留唯一事实来源的唯一点。 If we want to keep a separate list for both routes then we can directly use state which I think is not required here. 如果我们要为两个路由保留单独的列表,那么我们可以直接使用我认为此处不需要的状态。
  2. Fetching of the list should be done in componentDidMount of the first route and in route 2 you can again fetch the list and compare the data based on some checksum if we have new data or not if we have new data update the original list. 列表的获取应在第一条路线的componentDidMount中完成,而在路线2中,如果我们有新数据,则可以再次获取列表并基于某些校验和比较数据,如果有新数据则更新原始列表。 In this, we don't have the problem of stale data too. 在此,我们也没有过时数据的问题。

Maybe you can have a different use case but check if this approach works. 也许您可以使用其他用例,但请检查此方法是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM