Skip to content

Instantly share code, notes, and snippets.

@abdennour
Last active July 24, 2018 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdennour/7726cc000b4f490f1dd6e8251d010469 to your computer and use it in GitHub Desktop.
Save abdennour/7726cc000b4f490f1dd6e8251d010469 to your computer and use it in GitHub Desktop.
Array instead of mapStateToProps function in React-Redux connect
import { connect } from 'react-redux';
import _ from 'lodash';
export default function(...args) {
return function(ComponentClass) {
const mapStateToProps = !Array.isArray(args[0])
? args[0]
: state => _.pick(state, args[0]);
return connect(mapStateToProps, mapDispatchToProps)(ComponentClass);
};
}
// Using this gist, just two lines:
import connect './connect'
export default connect(['persons'])(YourComponent)
// Using connect of react-redux directly
import {connect} from 'react-redux';
function mapStateToProps({persons}) {
return {persons};
}
export default connect(mapStateToProps)(YourComponent)
@PabloMarch
Copy link

PabloMarch commented Jul 24, 2018

export default connect(({persons}) => ({persons}))(YourComponent)

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