-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
docs(store): add docs for selector with props #1233
docs(store): add docs for selector with props #1233
Conversation
docs/store/selectors.md
Outdated
); | ||
``` | ||
|
||
See the [StackBlitz example](https://stackblitz.com/edit/create-selector-props?file=src%2Fapp%2Freducers%2Findex.ts). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the StackBlitz example link. If we're going to link to live examples, we need to come up with a way to version and generate them from the repo.
docs/store/selectors.md
Outdated
|
||
Keep in mind that a selector only keeps the previous input arguments in its cache. Meaning that if you would re-use this selector but with another multiply factor, the selector would always have to re-evaluate its value. This is because it's receiving both of the multiply factors (e.g. one time `2`, the other time `4`). In order to correctly memoize the selector we can wrap the selector inside a factory function, so that the component can create its own instance of the selector. | ||
|
||
If we add multiple counters in the above example (the counters are differentiated by `id`), our component's selectors are now calling the factory function to create different selector instances: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following is an example os using multiple counters differentiated by id
. The component's selectors ...
docs/store/selectors.md
Outdated
} | ||
``` | ||
|
||
And the selector becomes: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example should be above the counter factory example
docs/store/selectors.md
Outdated
} | ||
``` | ||
|
||
The last argument of a selector or a projector is the `props` argument, for our example it looks as follows: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this example above the previous one to show how you create a selector with props
docs/store/selectors.md
Outdated
@@ -79,6 +79,53 @@ export const selectVisibleBooks = createSelector( | |||
); | |||
``` | |||
|
|||
### createSelector with props | |||
|
|||
Sometimes you want to select a piece of store state based on data that isn't available in the store state, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"To select a piece of state ... isn't available in the store, pass props
to the selector function."
docs/store/selectors.md
Outdated
); | ||
``` | ||
|
||
Keep in mind that a selector only keeps the previous input arguments in its cache. Meaning that if you would re-use this selector but with another multiply factor, the selector would always have to re-evaluate its value. This is because it's receiving both of the multiply factors (e.g. one time `2`, the other time `4`). In order to correctly memoize the selector we can wrap the selector inside a factory function, so that the component can create its own instance of the selector. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Meaning that if you would..." -> "If you re-use this selector with another..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"we can wrap" -> "wrap"
ba0e7df
to
442b167
Compare
@timdeschryver I have a question just to see if I'm understanding this. What would the benefit of the "props" syntax be over just passing parameters into a selector function? For example, what's the difference between the following two versions: Version 1:
Version 2
|
In the example you wrote the behavior will be the same. |
Ah, got it, thanks! |
No description provided.