我应该在每个路由器导航上重置我的redux存储吗? [英] Should I reset my redux store on each router navigation?

查看:137
本文介绍了我应该在每个路由器导航上重置我的redux存储吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular编写应用程序,并且通过 ngrx/store 使用 Redux 存储我的应用程序状态.

I'm using Angular to write my application, and I use Redux through ngrx/store to store my application state.

一切正常,但是现在我的应用程序变得很大,我得到了以下观察结果:

Everything work fine, but now that my application get pretty large, I came with the following observation:

对于几乎每个页面/资源,我都使用一个从后端获取的不同解析器,以正确显示此特定页面并在该特定页面上执行操作所需的所有数据.

For nearly each page/resource, I use a different resolver that fetch from my backend, all the data necessary to correctly display and act on this particular page.

此解析器使我可以使该页面独立于应用程序的先前状态.

This resolver allow me to make this page independent from the previous state of the application.

因此,如果应用程序已加载并从中启动,则说: https://myapp.com/resourcetype1/resourceId

So if the application is loaded and started from, let's say: https://myapp.com/resourcetype1/resourceId

解析器将加载ID为 resourceId resourcetype1 ,以便我的页面开箱即用.

The resolver will load the resourcetype1 with id resourceId in order for my page to work out of the box.

知道, 我想知道在每个路由器导航起点上完全重置我的redux存储是否是个好主意 .

Knowing that, I was wondering if it could be a good idea to fully reset my redux store on each router navigation start.

我认为这可能有两个好处:

I think this could have two benefits:

  • 提供一个更干净的redux存储,而没有以前执行的页面中的残留数据.
  • 在每个页面显示"之前提供一个空存储,这可以帮助检测页面何时使用了先前加载的页面中的数据(因此,如果使用其urm直接"加载,则将导致崩溃). >
  • Provide a cleaner redux store without residual data from a previously executed page.
  • Provide an empty store before each 'page display', which could help to detect when a page is using data from a previously loaded page (and so which would have crashed if loaded 'directly' using its urm).

所以问题是:

  • 您如何看待这种反思?
  • 您对此有何习惯?
  • 您是否要重置Redux存储区?

推荐答案

这确实取决于您的应用程序!

It really depends on your application!

例如,如果您使用Redux是因为每个页面都有一组要处理的复杂数据,而且每个页面都与其他页面完全独立,那么您可能希望在每次更改之间都将其清除.

For example, if you're using Redux because every page has a set of complex data to handle, but also every page is totally independent from the others, maybe you might want to clean it between every change.

但是感觉有点奇怪.您正在谈论的资源是什么?您不会回到以前的资源吗?

It feels a bit strange though. What's the resource you're talking about? Will you not come back to a previous resource?

在我的一个应用程序中,确实有工作区.每个工作空间可以包含大量数据.用户不会经常更改他们的工作空间,并且当他们更改工作空间时,可能不会回到前一个工作空间.因此,每当用户导航到另一个工作区时,我都会(几乎)清理所有的reducer.

Within one of my applications, I do have workspaces. And each workspace can contain a lot of data. Users will not change very often their workspace and when they do, it's probably not to come back on the previous one. So whenever a user navigate to another workspace, I clean (nearly) all my reducers.

但是,只要用户在同一个工作空间中,我就会保留大多数信息.这很棒,因为在刚开始用户选择工作区时,它几乎什么也没有.然后,当他浏览一些细节时,应用程序正在获取数据并将其存储在相应的reducer中.

But, as long as a user is on the same workspace, I keep most of the information. This is great because at the very beginning when a user just selected a workspace, it barely has nothing in it. Then, when he navigate inside some details, the application is fetching data and storing them within the corresponding reducer(s).

因此,当用户返回查看某些数据时,第一次在应用程序获取数据时出现等待时间,第二次和+时,用户会立即看到存储中已经存储的内容.当然,它仍会尝试获取数据以确保它是最新的,但是如果没有更改,则用户可以呼吸一下.

So when the user come back to see some data, the first time there's a latency while the application is fetching the data and the second time and +, user sees what's already into the store (instantly). Of course, it still tries to fetch the data to make sure it's up to date, but if it didn't change, the user is able to interact with it in a breathe.

这篇关于我应该在每个路由器导航上重置我的redux存储吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆