-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
example of the selector used in multiple component instances #1515
Comments
The example was contributed by a user as we were putting together the docs for the hooks API. I'd say it's largely because most folks are used to seeing selectors explicitly called with To be honest, I don't like this entire example at all. The naming is overly long, and it doesn't feel like there's enough real memoizing going on here to justify the complexity. |
@markerikson thanks a lot for your quick reply. I totally agree that a more relevant and clear example would be more beneficial for developers. Honestly I have a more complex use case when you have a big state object and you need to extract slice of data by the object key property which is So in my case I wanted to double check that I'm not missing any performance optimization or hidden redux cache functionality if you pass a dynamic property into Is |
I'd like to come up with a better example, but my current focus is on reworking the Redux core docs (per reduxjs/redux#3592 ). For reference, I have an extensive post on selectors at Using Reselect Selectors for Performance and Encapsulation. I'd really like that post turned into a complete docs page, but never gotten around to it. If you or someone else would like to try putting together a better example for the docs, I'd be very interested. |
@markerikson thanks a lot again. I will most definitely read the article you provided. I will close this thread since it is pretty much resolved. Also about the example - I can totally give it a shot in the next few days or over the weekend. Or suggest at my current company if anyone has a great example in mind. Do you have a specific example in mind or you want people to suggest theirs? |
We might as well keep this issue open to cover that discussion. |
@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: const { user, isFetching } = useSelector(state => state.profilePage, shallowEqual) // profilePage consists of { user, isFetching } in the redux state vs const user = useSelector(state => state.profilePage.user)
const isFetching = useSelector(state => state.profilePage.isFetching) My sample is heavily inspired by the conversation described here #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 |
The main question is whether there are any other fields in 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 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 |
oh crap, my bad! I forgot |
In that case the results should be basically equivalent. |
@markerikson but is there a more idiomatic preferred option? I see that there is more and more state splitting when people use |
Once the behavior is equivalent, no, it's really up to you, same as writing one large |
@markerikson before even starting to write any code I just wanted to propose an idea: Then we could add a re-usable reducer for a single employee as well as introduce a new visual component Employee Card which suppose to show only limited information about the person (Avatar, First Name and Last Name). This new component will explain use of selectors, selectors which require access to specific values instead of the whole object (performance note). Another idea I had is something related to map pins and extra meta data associated with them. |
I'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
Can I ask why the team decided to give this example instead of curried function and partial application?
Is there some performance/cache implications? Or it is just easier to read and understand for everyone who is reading this documentation?
The text was updated successfully, but these errors were encountered: