Map State to Props (using arrow function)

Map State to Props (using arrow function)
0

#1

Tell us what’s happening:
Tried writing this in arrow function, but it doesn’t work. Not sure why…

Your code so far


const state = [];

// change code below this line
/*function mapStateToProps(state) {
  return {
    messages: state
  }
}
*/
var mapStateToProps = (state) => {messages: state};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react-and-redux/map-state-to-props


#2

you forgot the brackets

var mapStateToProps = (state) => ({messages: state});

(the brackets are necessary when you are writing short-hand functions)


#3

As @hbar1st points out you must wrap the curly brackets with parentheses ( ), because when you use arrow function syntax => { }, JavaScript interprets the { } as a code block instead of something to return. Surrounding the { } lets JavaScript know you want to return what is inside the ( ).

Otherwise, you would have needed to add a return statement like below, which makes the arrow function less concise than it could be.

var mapStateToProps = (state) => { return {messages: state} };

#4

Thanks @hbar1st & @RandellDawson!

I have a better understanding now