Skip to content

Instantly share code, notes, and snippets.

@andycarrell
Last active September 13, 2017 22:54
Show Gist options
  • Save andycarrell/86a26c0ca18dd52c2aa122d318976c2a to your computer and use it in GitHub Desktop.
Save andycarrell/86a26c0ca18dd52c2aa122d318976c2a to your computer and use it in GitHub Desktop.
import { connect } from 'react-redux';
import * as selectors from '../selectors';
const fnNameFor = ([first, ...rest]) => (`get${first.toUpperCase()}${rest.join('')}`);
const selectFor = key => selectors[fnNameFor(key)];
const getMapStateFor = stateKeys => state => stateKeys.reduce(
(acc, key) => ({ ...acc, [key]: selectFor(key)(state) }),
{},
);
export const connectWithState = (...stateKeys) => connect(getMapStateFor(stateKeys));
@andycarrell
Copy link
Author

Usage:

export default connectWithState(
    'isFetching',
    'users',
)(Cmp);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment