Last active
July 24, 2018 15:24
-
-
Save abdennour/7726cc000b4f490f1dd6e8251d010469 to your computer and use it in GitHub Desktop.
Array instead of mapStateToProps function in React-Redux connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
export default connect(({persons}) => ({persons}))(YourComponent)