Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React. Why is the return inside a map, not the other way around?

I’m learning react and reading this code: https://github.com/john-smilga/react-advanced-2020/blob/master/src/tutorial/4-forms/final/1-controlled-inputs.js (long…)

What I don’t understand is this part (line 53-61):

    {people.map((person, index) => {
      const { id, firstName, email } = person;
      return (
        <div className='item' key={id}>
          <h4>{firstName}</h4>
          <p>{email}</p>
        </div>
      );
    })}

Why is the return inside a map ? Since the map is taking items from the array and operating on them individually, won’t there be multiple returns for each operation ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank very much !

>Solution :

const l = [1, 2, 3]

const mapped_array = l.map(li => {
    console.log(li) // 1, 2, 3
    return 10 * li  
})

console.log(mapped_array)
/// [10, 20, 30]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading