I'm learning the usage of reselect, but i'm confused about the way i make use of the memoized selector in inside my container.
I've got a component called Summary
that received 3 props thats are subtotal, tipAmount , total
.
It looks like this export const Summary = ({ subtotal = 0, tipAmount = 0, total = 0 })=>{...}
.And those props are being inject my the connect HOC
inside a dedicated container
that i've called SummaryContainer
with the following code.
import { connect } from 'react-redux';
import { Summary } from '../components/Summary';
import {
selectTipAmount,
selectSubtotal,
selectTotal
} from '../store/items/selectors';
const mapStateToProps = (state) => {
const subtotal = selectSubtotal(state);
const tipAmount = selectTipAmount(state);
const total = selectTotal(state);
return {
subtotal,
tipAmount,
total
};
};
export const SummaryContainer = connect(mapStateToProps)(Summary);
As you can see this code uses 3 selectors selectTipAmount
, selectSubtotal
and selectTotal
that i'm importing from a selector file with the follwing code.
import { createSelector } from 'reselect';
const selectItems = (state) => state.items;
const selectTipPercentage = (state) => state.tipPercentage;
export const selectSubtotal = createSelector([selectItems], (items) =>
items.reduce((acc, { price, quantity }) => acc + price * quantity, 0)
);
export const selectTipAmount = createSelector(
[selectSubtotal, selectTipPercentage],
(subtotal, tipPercentage) => subtotal * (tipPercentage / 100)
);
export const selectTotal = createSelector(
[selectSubtotal, selectTipAmount],
(subtotal, tipAmount) => subtotal + tipAmount
);
export const selectItemWithTotal = createSelector([selectItems], (items) =>
items.map((item) => ({ ...item, total: item.price * item.quantity }))
);
export const selectItem = (state, props) =>
state.items.find((item) => item.uuid === props.uuid);
export const selectItemTotal = createSelector(
[selectItem],
(item) => item.price * item.quantity
);
So here is my problem :
When i call write const total = selectTotal(state);
inside mapStateToProps
i passe to the total
constant the results of the execution of selectTotal(state) selector
and when i look at the argument
, it's state
.It looks like i'm invoking the selectTotal selector
by passing and state
argument, but in the actuel implementation it isn't a function that takes a state param
.In the actual implementation that selection equals the result of createSelect function
like this:
export const selectTotal = createSelector(
[selectSubtotal, selectTipAmount],
(subtotal, tipAmount) => subtotal + tipAmount
);
I haven't seen where the state argument
is being passed.What is going on?
The code works perfectly but i'm lost about where the state
argument is being used.
When i look at selectItems selector
it makes sens, but with selectors that are created with createSelector
i'm lost.