reselect
reselect copied to clipboard
How to apply a selector with others in the connect decorator?
From the documentation I can't seem to figure out how to combine my selector with the other connect parameters.
This is my selector
import { createSelector } from 'reselect'
const optionsSelector = state => state.options
const valuesSelector = state => state.values
const currentGroupSelector = state => state.currentGroup
export const currentOptionsWithValuesSelector = createSelector(
[optionsSelector, valuesSelector, currentGroupSelector],
(options, values, group) => {
const opts = options.filter(opt => opt.group === group)
// create an array of new option object where value is merged in
const result = opts.map((opt) => ({ ...opt, value: values.get(opt.id) }))
return {currentOptionsWithValues: result}
}
)
Now I would like to pass both the state filter data and the selector result to my component. I tried to make it work like this:
@connect(
state => (Object.assign({
filter: state.filter
}, currentOptionsWithValuesSelector(state.filter)))
)
export default class MyComponent extends Component {
}
Apart from that this would be ugly and hard to read it doesn't seem to work either. How can I add the selector to the connect (in an elegant way)?
How about this?
import { createSelector } from 'reselect'
const optionsSelector = state => state.options
const valuesSelector = state => state.values
const currentGroupSelector = state => state.currentGroup
const filterSelector = state => state.filter
const currentOptionsWithValuesSelector = createSelector(
[optionsSelector, valuesSelector, currentGroupSelector],
(options, values, group) => {
const opts = options.filter(opt => opt.group === group)
// create an array of new option object where value is merged in
const result = opts.map((opt) => ({ ...opt, value: values.get(opt.id) }))
return {currentOptionsWithValues: result}
}
)
export allTheThingsSelector = createSelector(
[currentOptionsWithValuesSelector, filterSelector],
(currentOptionsWithValues, filter) => {
return {
...currentOptionsWithValues,
filter
}
}
);
@connect(allTheThingsSelector)
export default class MyComponent extends Component {
}