Return a calculated value instead of an object with a calculated value
Hi,
Is it allowed to create an exposed selector which returns a calculated value, instead of the calculated value wrapped inside an object?
e.g.
// this selector will return a calculated value, istead of `{ key: calculatedValue }`
export const optionsArraySelector = createSelector(
(filter) => Object.keys(filter.options).map(id => options[id])
)
@connect(
state => ({
// now we're able to define the prop's name
options: optionsArraySelector(state.filter)
})
)
export default class ...
createSelector needs to take at least two arguments. For optionsArraySelector you are missing a function that supplies the filter argument to your results function.
export const optionsArraySelector = createSelector(
state => state.filter,
filter => ({
options: Object.keys(filter.options).map(id => options[id]);
})
)
@connect(optionsArraySelector)
export default class ...
I might be missing something, but I think the above code matches the intent of your example. Please let me know if I have misunderstood.
Ah yes, case of bad copy-paste from my code repo; however the main question is still unanswered: I was wondering if it's allowed to return the Object.keys(filter.options).map(id => options[id]) instead of wrapping it inside an object:
export const optionsArraySelector = createSelector(
state => state.filter.options,
options => Object.keys(options).map(id => options[id])
)
@connect(
state => ({
SomePropertyNameWhichContainsTheOptions: optionsArraySelector(state)
})
)
export default class ...
However, now I realize there's actually another question I've got, which may answer the first one:
Is it suggested to create a single selector per component, or to mix and match selectors inside a component?
Let me explain with some examples:
// we'll first create some selectors
export const optionsArraySelector = createSelector(
state => state.filter.options,
options => Object.keys(options).map(id => options[id])
)
// some other selector
export const someOtherValueSelector = createSelector(
state => state.some.other.value,
value => value * 2 // or whatever
)
// Now imagine there's a component depending on both the
// `optionsArray` as `someOtherValue`:
// implementation #1: we can create a selector specific for this case:
export const mergedSelectors = createSelector(
[optionsArraySelector, someOtherValueSelector],
(options, someOtherValue) => ({ options, someOtherValue })
)
// and use this `mergedSelector` with our component:
@connect(mergedSelectors)
export default class ...
// implementation #2: we could choose some selectors per component and assign
// it's returned values manually to keys in the `props` (at component-level)
// (in this case the selectors return the "value", instead of an object with the value)
@connect(
state => ({
options: optionsArraySelector(state),
someOtherValue: someOtherValueSelector(state)
})
)
export default class ...
// implementation #3: or, if we make sure all selectors return objects,
// we could apply the result like this:
@connect(
state => ({
...optionsArraySelector(state),
...someOtherValueSelector(state)
})
)
export default class ...
It might be a little bit of a messy story, but I hope you see/understand the difference between returning an object vs returning the actual value :-)
Yeah, you can certainly return a primitive value from a selector if you want. Personally I would go with implementation 1 and create mergedSelectors, but I think this is just a matter of taste.
Well, that answered my question :-) Thanks!