From 3fed2879abcb5b875a4c9d00018320eb0ebc99aa Mon Sep 17 00:00:00 2001 From: James Allen Date: Tue, 14 May 2024 16:53:33 +0100 Subject: [PATCH 1/2] Added an example of creating a selector with arguments to README Based on [#11](https://github.com/brianegan/reselect_dart/issues/11) --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 13b43f2..abd0304 100644 --- a/README.md +++ b/README.md @@ -198,3 +198,31 @@ final onSaleSelector = createSelector1( }, ); ``` + +### Selectors with arguments + +To create a selector which takes arguments, you have a few options. First, you can write a fuction that creates a selector. Let's say you wanted to make the affordable products selector configurable: + +```dart +Selector> createAffordableProductsSelector(double price) { + return createSelector1( + productsSelector, + (products) => products.where((product) => product.price < price), + ); +} + +// Usage +final affordableProductsSelector = createAffordableProductsSelector(10.00); +final affordableProducts = affordableProductsSelector(appState); +``` + +The other option: Although a selector only takes in 1 parameter, that parameter can be anything you want. It can be a class that holds multiple values, or a Dart 3 record. + +```dart +typedef Params = (AppState state, double price); + +final affordableProductsSelector = createSelector1, double), List>( + (params) => (productsSelector(params.$2), params.$2), + (result) => result.$1.where((product) => product.price < result.$2), +); +``` From ec009ca914d8cf0048d11cb301385673b2e3b00b Mon Sep 17 00:00:00 2001 From: James Allen Date: Tue, 14 May 2024 17:11:19 +0100 Subject: [PATCH 2/2] Fixed incorrect record index in second example of selectors with arguments in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index abd0304..0d63b20 100644 --- a/README.md +++ b/README.md @@ -222,7 +222,7 @@ The other option: Although a selector only takes in 1 parameter, that parameter typedef Params = (AppState state, double price); final affordableProductsSelector = createSelector1, double), List>( - (params) => (productsSelector(params.$2), params.$2), + (params) => (productsSelector(params.$1), params.$2), (result) => result.$1.where((product) => product.price < result.$2), ); ```