Reputation: 598
I have a simple need in my React-Redux app that I would like to find the 'redux way' of dealing with:
The first time that the app is run, some random data is generated and passed in as initialState
and throughout the execution of the app, I'm playing with that data using redux action dispatchers and state changes.
However, I need to calculate some derived data based off of the first received state inside my mapStateToProps
function and use it through the end of the app execution. This would have been easy if the derived data had to be updated based on every state change however the trick is that I will only need to calculate it once and memorize it throughout the app execution.
The derived data is not a logical part of the app state so I wouldn't want to bother making it part of the store. I could use a simple global variable to save this data and avoid recalculating it at a later point but I'm almost sure this is not the 'redux way'! using selectors also doesn't seem to be the answer as it recalculates the derived data with every state change. I've also looked into React context but as I don't want to send my data to components right away, it also doesn't seem to be what I need.
Any suggestions?
Upvotes: 0
Views: 477
Reputation: 4375
You should store it in Redux. Maybe it gets modified only once, but it still is application state.
Note that anything that can be derived from the current app state should be calculated "on the fly" using a selector.
By using Reselect you don't have to worry about performance issues (unless the deriving takes a very long time and you'd need WebWorkers).
Upvotes: 1