Skip to content

createSelector call other selector with parmeter returned from the state #3499

Answered by david-shortman
nep7un asked this question in Q&A
Discussion options

You must be logged in to vote

Two ways to go about this. One, I explained in this thread. You can create a selector that delays the collection of a parameter.

Second, you could get a reference to the global state and provide it to the selector:

export const selectProductByIdWithRelatedProducts = (key: string) => createSelector(
    state => state, // select global state
    selectProducts,
    selectProductById(key),
    (globalState, productState, currentProduct) => {
        if (currentProduct && currentProduct.collection && productState) {
            return {...currentProduct, relatedProducts: selectProductsByCollection(currentProduct.collection.key)(state)}
        }
    }
);

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@nep7un
Comment options

Answer selected by nep7un
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants