Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept object as connect's mapStateToProps argument? #594

Closed
alsiola opened this issue Jan 3, 2017 · 1 comment
Closed

Accept object as connect's mapStateToProps argument? #594

alsiola opened this issue Jan 3, 2017 · 1 comment

Comments

@alsiola
Copy link

alsiola commented Jan 3, 2017

We can currently call connect with an object argument for mapDispatchToProps, where each function in the object is assumed to be an actioncreator. Could a similar option be available for mapStateToProps, where it would be assumed that each function in the object would be a selector? This could then be used as, for example:

import { connect } from 'react-redux';
import { actionCreator1, actionCreator2 } from './ActionCreators';
import { selector1, selector2 } user from './Selectors';
import PresentationalComponent from './PresentationalComponent';

export default connect(
    { 
        selector1,
        selector2
    },
    {
        actionCreator1,
        actionCreator2
    }    
)( PresentationalComponent );

I am currently achieving this with the following:

import { connect } from 'react-redux';

export default (originalStateToProps, dispatchToProps) => {

    let updatedStateToProps = originalStateToProps;

    if (typeof originalStateToProps !== "function") {
        updatedStateToProps = (state, ownProps) => {
            let mappedProps = {};
            for (const key in originalStateToProps) {
                if (originalStateToProps.hasOwnProperty(key)) {
                    mappedProps[key] = originalStateToProps[key](state, ownProps);
                }
            }
            return mappedProps;
        }
    }   
    
    return connect( updatedStateToProps, dispatchToProps );
}
@jimbolla
Copy link
Contributor

jimbolla commented Jan 3, 2017

See #323

@timdorr timdorr closed this as completed Jan 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants