In RTK, how to run an expensive function only when a portion of a nested state prop (array of multiple nested objects) changes
87sam12 opened this issue · 1 comments
What is the current behavior?
Not sure whether RTK currently supports this.
What is the expected behavior?
In RTK, How to run an expensive function only when a portion of a nested state prop (array of multiple nested objects) changes. Lets say some dispatched action triggers an api call to receive some data using which the state is updated using a reducer. Now I want another expensive function that computes some data from this state portion which is accessed by UI. I want this function to run only when this data received is different than its prior data. Does RTK offers to subscribing to changes?? Does the component level mapstatetoprops sense these updates? Please clarify. I already tried some other channels. Not got a satisfactory response yet.
I feel like usememo (react hook) might not be appropriate. Lets assume my state portion is of the form (array of 100 elements). I need to run an expensive long-running function only when something in this array changes:
[{'key1':'val1','key2':'val2'} , {'key1':'val1','key2':'val2'}, .....]
you would need to use a memoised selector: Deriving Data with Selectors
createSelector
is re-exported from RTK for your convenience, but other memoisation options such as proxy-memoize are available.