-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Description
Describe the bug
The select option function is only executed if data (the input to that function) changes. We can see this here, the previous select result is used if the result data hasn't changed:
But if you have any other dependency from a closure to that select function, it will not be executed even though a "dependency" to it changes.
To Reproduce
https://codesandbox.io/s/serverless-cdn-3besp?file=/src/index.js
- Click on the button, which will increment the
count countis used in theselectfunction, but it is not executed
Expected behavior
select to be executed on every render
Desktop:
- OS: macOS Catalaina
- Browser: chrome
- Version: 3.5.12
Additional Context
Another interesting fact seems to be that we are dependent on the data of the observer (this.currentResult), which means that if you unmount your component, it will execute the selector again, even though we still have data in the cache. See https://codesandbox.io/s/magical-currying-jo0j9?file=/src/index.js
- click the
Incbutton, nothing happens - click
Remount-> select now executes.
This can happen when you do route changes for example. It's a bit undeterministic when the selector function actually runs, so I think it would be better to err on the side of caution and always re-run it. The optimization of not running the selector function in some situations seems a bit premature, If I'm not mistaken, redux also runs the selector on every render, and the suggested solution would be to memoize selectors yourself, e.g. with reselect. Usually, running the selectors is not very expensive.
@boschni FYI