redux - Define selector dynamically based on result previous selection -
i have selector gets current filter type store (filterselector).
, 3 more, filter entities store using different logic, let's imagine names smallentitiesselector, mediumentitiesselector, bigentititiesselector.
what best way create selector, return filtered entities based on filter values in store?
const filteredentities = createselector( filterselector, (filtertype) => { switch filtertype: case "small": ??? case "medium": ??? case "big": ??? } )
for particular example create 1 selector, accepts filter parameter, in real case have more complex logic inside selectors , don't want mix them.
upd: due request in comments add more code. here way how make work:
const filteredentities = createselector( filterselector, smallentitiesselector, mediumentitiesselector, bigentititiesselector, (filtertype, smallentities, mediumentities, bigentities) => { switch filtertype: case "small": return smallentities; case "medium": return mediumentities; case "big": return bigentities; }
)
but don't solution, because 3 selectors executed every time original entities changed, while need 1 executed. ti works, feel there way better.
as first attempt, might set 2 selectors derive current filter , current entities data. then, call proper selector directly result function.
const filteredentities = createselector( filterselector, entitiesselector, // todo (filtertype, entities) => { switch filtertype: case "small": return smallentities(entities); case "medium": return mediumentities(entities); case "big": return bigentities(entities); // case default? } );
if step performance/cache invalidation issues, re-reselect might you.
import createcachedselector 're-reselect'; const filteredentities = createcachedselector( filterselector, entitiesselector, // todo (filtertype, entities) => { switch filtertype: case "small": return smallentities(entities); case "medium": return mediumentities(entities); case "big": return bigentities(entities); // case default? } )( filterselector );
Comments
Post a Comment