reselect
reselect copied to clipboard
[Documentation/Support] using mapping the result of a selector with another selector.
Background
I have a balance adapter that has 10 elements and a market adapter that has many more (let's say 100). Now I want to create derived data that gets the fiat balance by multiplying my balance adapter by its USD market.
// Typing on phone so might get syntax wrong
const selectBalaceCurrencies = balanceAdapter.getAll() // returns ['USD', 'EUR']
const selectBalaceByCode = balanceAdapter.getEntityById() // returns {code: 'USD', available: 10 }
const selectPriceByCode = priceAdapter.getEntityById() // Returns { askPrice, dailyChange, ...ext }
// What I want to do is
const selectLiveBalance = (state: RootState) => {
const currencies = selectCurrencies(state)
const liveBalance = currencies.map((code) => {
const balance = selectBalaceByCode(state, code)
const price = selectPriceByCode(state, code)
return balance.available * price.askPrice
}
}
I have managed to merge the second two into one selector that's caching properly selectLiveBalanceByCode
but I can't for the life of me work out how to get that to merge with the first selector and only update when one of the selected codes change not when any state changes or I could do it when any reference to price changes (by selecting the object adapter.getEntityRecord() //the one that returns an object
but then my balance updates when a market price changes for something that we don't have a balance for.
Goal
I want to only update my derived balance when the selectPriceByCode (merged with balance it's selectLiveBalanceByCode
) updates with values from selectBalanceCurrencies
I have given up for the day but can't stop thinking about it (hence typing this on phone instead of at desk)
Hope their is a solution and I have provided enough details