-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
How to get updated EntityAdapter / EntitySelectors after CRUD operation to get synced operations #452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hm, that's an interesting one. As you are within a reducer, that reducer is wrapped in a Clearly we didn't take into account that someone would use the selectors within a reducer. @markerikson we could make the selectors use |
@smajl could you report if this fixes your problem? I'm not sure if calling +import { original } from 'immer';
const adapter = createEntityAdapter();
const selectors = adapter.getSelectors(state => state.some);
...
initialState: adapter.getInitialState(),
reducers: {
remove(state, action) {
- console.log(selectors.selectIds(state).length); // let's say it's 10
+ console.log(selectors.selectIds(original(state)).length); // let's say it's 10
adapter.removeMany(action.payload.ids); // ids = [1, 2, 3]
- console.log(selectors.selectIds(state).length); // I would expect 7, but it's still 10 !
+ console.log(selectors.selectIds(original(state)).length); // I would expect 7, but it's still 10 !
},
... |
Hmm. It doesn't seem so, function defaultEqualityCheck(currentVal, previousVal) {
return currentVal === previousVal && !isDraft(currentVal)
} But that would lead to new values being generated even if there is no need for it, possibly causing referential inequality of otherwise equal values down the road. I'll open an issue over at |
@smajl I just noticed what probably was an error creating this example: You are calling |
@phryneas Yes, I'm using correct call with state, it was just a "typo" when I was rewriting original into this example. Anyway, thanks. Btw, my use-case: Tabs list with selected tab index pointer. After removing N tabs from the list, I need to move that index pointer to closest suitable tab based on some logic. All in one reducer. In the meantime I simplified and refactored my code, so it does not rely on that second selectors call. :) And since I'm now using your nifty EntityAdapter, I may just use tab IDs instead of index and compute index on the fly with |
Good you are finding a way around this for yourself - but you definitely found a tricky potential bug here, so thanks for the report - and we'll continue to try to fix this :) |
Given the proxy issue, I'm not sure there's much we can do here. This seems like an unfortunate collision between how Immer works and how Reselect works, and abstractions are leaking. To be honest, |
@markerikson So the suggested thing to do here is to just directly access your entity adapter |
Given that the selectors don't provide any benefit here in a reducer, and that you're seeing this issue, yes. |
Here is my solution. |
I meet the same problem https://codesandbox.io/s/intelligent-proskuriakova-198ge The data in nextState has been changed,but the selectById function seems cached the prevState. Does pure function much better than a cached function? |
Consider this pseudo example:
I am confused, am I missing something? Do I need to generate new set of selectors after each CRUD operation?
The text was updated successfully, but these errors were encountered: