reselect icon indicating copy to clipboard operation
reselect copied to clipboard

How to apply a selector with others in the connect decorator?

Open 0x80 opened this issue 11 years ago • 1 comments

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)?

0x80 avatar Aug 30 '15 13:08 0x80

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 {

}

ellbee avatar Aug 31 '15 09:08 ellbee