问题
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.
So if the application is loaded and started from, let's say: https://myapp.com/resourcetype1/resourceId
The resolver will load the resourcetype1 with id resourceId in order for my page to work out of the box.
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:
- 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).
So the questions are:
- What do you think about this reflexion?
- What are your habit concerning that?
- Are you resetting your redux store at some point or no?
回答1:
It really depends on your application!
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?
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.
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.
来源:https://stackoverflow.com/questions/44120080/should-i-reset-my-redux-store-on-each-router-navigation