example of the selector used in multiple component instances
See original GitHub issueI’m aware that this is a bug tracker and not a support system. But there is one place in the official documentation which puzzles me. I just wonder if react-redux
team has decided to pick one implementation over another for a specific reason or else.
https://react-redux.js.org/next/api/hooks#useselector-examples
const makeNumOfTodosWithIsDoneSelector = () =>
createSelector(
state => state.todos,
(_, isDone) => isDone,
(todos, isDone) => todos.filter(todo => todo.isDone === isDone).length
)
const selectNumOfTodosWithIsDone = useMemo(
makeNumOfTodosWithIsDoneSelector,
[]
)
const numOfTodosWithIsDoneValue = useSelector(state =>
selectNumOfTodosWithIsDone(state, isDone)
)
Can I ask why the team decided to give this example instead of curried function and partial application?
const makeNumOfTodosWithIsDoneSelector = () => isDone =>
createSelector(
state => state.todos,
todos => todos.filter(todo => todo.isDone === isDone).length
)
const selectNumOfTodosWithIsDone = useMemo(
makeNumOfTodosWithIsDoneSelector,
[]
)
const numOfTodosWithIsDoneValue = useSelector(selectNumOfTodosWithIsDone(isDone))
)
Is there some performance/cache implications? Or it is just easier to read and understand for everyone who is reading this documentation?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Selector for multiple instances - typescript - Stack Overflow
I am working on an application that can include multiple instances of components, for example, a component that reorders one data set into...
Read more >Understanding the Purpose and Use of the Selector in Angular
A selector is used to identify each component uniquely into the component tree, and it also defines how the current component is represented ......
Read more >Selectors - W3C
A type selector matches the name of a document language element type. A type selector matches every instance of the element type in...
Read more >Deriving Data with Selectors - Redux
Typical examples of this would look like selectTodoById ... Then, you can use a given selector function many times in the codebase, ...
Read more >Matching Multiple Selectors On The Same Element Creates A ...
Matching Multiple Selectors On The Same Element Creates A Single Directive Instance In Angular 5.0.0 · [everySecond] · [everyTwoSeconds].
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The main question is whether there are any other fields in
state.profilePage
.If those are the only two fields, and you want to re-render when either of those change, then sure, select the entire object and destructure.
However, say there many more fields in
state.profilePage
, but your component only cares about those two. Withstate => state.profilePage
, the component will re-render when theprofilePage
object itself is a new reference (ie, when any of the fields have been updated, assuming correct immutable updates). Withstate => state.profilePage.user
, it will only re-render when the.user
field has been updated.As I said in the other thread, always try to select the minimal amount of data needed by this component, and that is true for both
useSelector
andmapState
.@markerikson I will ask another very silly question here which is possibly would be a good idea to mention in the documentation as well. It is the difference between:
vs
My sample is heavily inspired by the conversation described here https://github.com/reduxjs/react-redux/issues/1459 Just instead of generic names I thought it might be more useful to be slightly more specific in this example.
@markerikson what would be a more idiomatic Redux way to select data in this case?
I see that the second approach is definitely more welcome for
useState
react hooks examples. Definitely highlights potentially re-usable chunks which could be put in their own re-usable custom hooks. But then I can also imagine of a case of getting 50 properties from the same state object… which at least will blow up code in sizes and makinguseSelector
trying to get properties of the same object again and again (potential performance issue when we talking about large production data?).