Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

what kinda function is this : const MapStateToProps = state => ()

what kinda function is this : const MapStateToProps = state => () why does it end with a PARENTHESIS instead of brace?

Vince Brown
Vince Brown
16,249 Points

Mrwan, It is just a regular arrow function but is using the shorthand syntax for return

since mapStateToProps is suppose to return an object instead of

const MapStateToProps = state => {
  return {
     name: state.user.name
     age: state.user.age
  }
}

You can write it like this

const MapStateToProps = state => ({
   name: state.user.name
   age: state.user.age
})

1 Answer

Steven Parker
Steven Parker
231,269 Points

As Vince's examples show, one form of function definition would have a function body in braces. Another form has a single expression which is returned.

The parentheses after the "fat arrow" make it clear that the second form is being used. This is particularly useful when the return value happens to be a literal object (as in Vince's examples). Otherwise, the braces might be misinterpreted as being part of the first form without them and cause an error.