1

Learning React and trying to cheat off this codepen. I do not understand 2 things about the map function in FormCard.

  1. Why does this .map function have a return statement, I did not see a return on other examples

  2. Why does the arrow function use curly braces instead of parentheses like the previous arrow function

const FormCard = (props) => (

  const FormCard = (props) => (
  <div>
    {
      DATA.map((props) => {
        return <div style={{...largebox, ...flex}} key={props.id}>
          <div style={{...Photo,backgroundImage: `url(${props.photo})`}}></div>
          <div>
            <Author author={props.author}/>
            <Something bio={props.bio}/>
            <AdBox adpic={props.adpic} />
            <IconBox />
          </div>
      </div>
      })
    }
  </div>
)
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98
Steve
  • 79
  • 1
  • 7

2 Answers2

8

These are two different ways of returning from arrow functions.

The implicit return:

If the body starts with an expression and not with a { is seen as a value to be returned.

[0,1,2,3,4,5,6].map(v => ({value:v})); // gives an array of objects with value set to v. 
[0,1,2,3,4,5,6].map(v => v*v)// gives an array of squares of the initial array. 

the explicit return:

If the body starts with a {, it seen as the body of the function, and a return statement is expected to return.

[0,1,2,3,4,5,6].map(v => { return {value:v}}); // gives an array of objects with value set to v. 
[0,1,2,3,4,5,6].map(v => { return v*v})// gives an array of squares of the initial array. 
DesTroy
  • 390
  • 4
  • 17
  • 1
    this answer makes the relevant distinction for flavors of arrow functions: expressions vice functions. – radarbob Aug 25 '18 at 18:37
4

Generally,

array.map((arg) => { return actionWith(arg) })
array.map((arg) => actionWith(arg))

Are equal, thus developers shrink their functions if they have returns only

Alex Antonov
  • 14,134
  • 7
  • 65
  • 142