Skip to content

Latest commit

 

History

History
45 lines (34 loc) · 1.19 KB

5-selectorator.md

File metadata and controls

45 lines (34 loc) · 1.19 KB

Usage with Selectorator

re-reselect accepts a selectorCreator option which allows to replace reselects's createSelector with any other implementation.

Wrap boilerplate code into a reusable create selector function

import {createCachedSelector} from 're-reselect';
import createSelectoratorSelector from 'selectorator';

export function createCachedSelectorWithSelectorator(...args) {
  return (polymorphicOptions = {}) => {
    const options = {
      selectorCreator: createSelectoratorSelector,
    };

    if (typeof polymorphicOptions === 'function') {
      options.keySelector = polymorphicOptions;
    } else {
      Object.assign(options, polymorphicOptions);
    }

    return createCachedSelector(...args)(options);
  };
}

Use selectorator in your app

import {createCachedSelectorWithSelectorator} from './createCachedSelectorWithSelectorator';

// selector created with single method call
const getBarBaz = createCachedSelectorWithSelectorator(
  ['foo.bar', 'baz'],
  (bar, baz) => `${bar} ${baz}`
)(({baz}) => baz);

const state = {
  foo: {bar: 'bar'},
  baz: 'baz',
};

console.log(getBarBaz(state)); // "bar baz"