Skip to content
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

createSelectorCreator uses defaultMemoize function for resultFunc #330

Closed
meganwalker-ibm opened this issue Mar 15, 2018 · 1 comment
Closed

Comments

@meganwalker-ibm
Copy link

meganwalker-ibm commented Mar 15, 2018

Here's my situation:

state: {
   "a": "foo",
   "b": {
      "c": "bar",
      "d": "baz"
     /* other properties inside `b` */
   }

(b is actually a far deeper series of nested objects (including b.c and every other property in b, but for simplicity sake I've left that out it here)

I have the following selector

createSelector(
   state => state.a,
   state => state.b.c,
   (a, c) => { /* some expensive computation here */ }
)

a changes rarely and is suitable for the default strict equality check.
However b changes frequently (and the nature of the changes mean b.c fails the strict equality check), yet b.c itself rarely has a meaningful change. This causes the selector to be recomputed a lot needlessly.

So I attempted to use createSelectorCreator with a deep equal function, as per the documentation. My first attempt resulted in the following selector code:

const createDeepEqualSelector = createSelectorCreator(
  defaultMemoize,
  equal
)

const cSelector = createDeepEqualSelector(
  state => state.b.c
)

createSelector(
   state => state.a,
   cSelector,
   (a, c) => { /* some expensive computation here */ }
)

However this still called my main selector every time b was updated, even if b.c was unchanged.

Eventually I got it working by changing cSelector to be

const cSelector = createDeepEqualSelector(
  state => state.b.c,
  c => c
)

Having looked at the code it appears as though the problem is the use of defualtMemoize here rather than the user provided memoize and memoizeOptions.

I might be wrong about this being the cause, as I haven't yet managed to understand fully how this library is functioning internally, but it seem intuitiviely suboptimal that I need to add a dummy function for this usecase to work.

@ellbee
Copy link
Collaborator

ellbee commented Jun 22, 2018

I think this should be address in v4 (#351)

@ellbee ellbee closed this as completed Jun 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants