Chris Foster Chris Foster - 3 months ago 11
React JSX Question

Why is `mapStateToProps` always an anonymous function?

In the official documentation for using React with Redux, the documentation seems to always define

mapStateToProps
as an anonymous arrow function assigned to a variable:

const mapStateToProps = (state, ownProps) => {
return {
active: ownProps.filter === state.visibilityFilter
}
}


This also seems to be the usage pattern for
mapDispatchToProps
and other functions passed to
connect
both in the docs and in the community.

It seems it would be much more simple, and follow traditional usage, to define it like so:

function mapStateToProps(state, ownProps) {
return {
active: ownProps.filter === state.visibilityFilter
}
}


Why is this done? Is there a practical reason for this, or is it just preference? Is it frowned upon to use a standard function definition?

Answer

No particular reason, it's just the style that was used in writing the docs. Feel free to write your mapState function as either arrows or standard function declarations, declared standalone or declared inline. No difference either way.