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

docs(store): add docs for selector with props #1233

Merged

Conversation

timdeschryver
Copy link
Member

No description provided.

@coveralls
Copy link

coveralls commented Aug 6, 2018

Coverage Status

Coverage remained the same at 88.618% when pulling 442b167 on timdeschryver:pr/selector-with-props-docs into d34b88a on ngrx:master.

);
```

See the [StackBlitz example](https://stackblitz.com/edit/create-selector-props?file=src%2Fapp%2Freducers%2Findex.ts).
Copy link
Member

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.


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:
Copy link
Member

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 ...

}
```

And the selector becomes:
Copy link
Member

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

}
```

The last argument of a selector or a projector is the `props` argument, for our example it looks as follows:
Copy link
Member

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

@@ -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,
Copy link
Member

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."

);
```

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.
Copy link
Member

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..."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"we can wrap" -> "wrap"

@timdeschryver timdeschryver force-pushed the pr/selector-with-props-docs branch from ba0e7df to 442b167 Compare August 8, 2018 10:27
@brandonroberts brandonroberts changed the title docs(Store): add docs for selector with props docs(store): add docs for selector with props Aug 16, 2018
@brandonroberts brandonroberts merged commit 617d198 into ngrx:master Aug 16, 2018
@timdeschryver timdeschryver deleted the pr/selector-with-props-docs branch August 16, 2018 12:28
@davidtlee
Copy link

davidtlee commented Aug 23, 2018

@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:

this.store.pipe(select(getCount, { multiple: 10 }))
export getCount = createSelector(
  getCounterValue,
  (counter, props) => counter * props.multiply
)

Version 2

this.store.pipe(select(getCount(10)))
export getCount = (multiply) => createSelector(
  getCounterValue,
  (counter) => counter * multiply
)

@timdeschryver
Copy link
Member Author

In the example you wrote the behavior will be the same.
The only difference is that props will also get passed to the getCounterValue selector.

@davidtlee
Copy link

Ah, got it, thanks!

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

Successfully merging this pull request may close these issues.

4 participants