Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript React Basics Introducing Props Iterating and Rendering with map()

Lacking explanation of array magically converted to JSX

The map function returns an array. In the video, this array is not put into an array variable, it just hangs in the air - without explaining how that array somehow ends up being inserted in the React component.

I assume this is some implicit (unexplained in any video?) functionality in JSX, that if you call a function within a JSX expression and it returns an array, then the contents of that array will be automatically converted to a JSX String and injected in the position the function occurs?

I just find this video really confusing due to the teacher skipping the explanation of this...but maybe I missed some point in some video explaining this...

I think I see the confusion. After the map() call is done, there's a new array available, but it has the tags, one per item in the initial array, but each item looks like this:

<Player
   name='Guil'
   score=50
/>

(that's the newly transformed players[0] item)

React is what turns that new array into each separate component and outputs the actual HTML code. That's pretty magical. If we didn't use map() to change each object in the array, React would ignore each item since it only knows how to render things that look like components (Player is one of the ones we defined.) In other words, our map() creates an array of calls to the Player component function, and the React engine looks at each item and runs the component code.

That code above gets translated (by React) to:

Player({name='Guil', score=50})

and that's what runs for each array item in the newly mapped array.

2 Answers

Torben Korb
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Torben Korb
Front End Web Development Techdegree Graduate 90,071 Points

Hi Iassek,

yes this seems confusing. But you are absolutely right with your explanation. The map method returns an array of components (Player here). Those kind of arrays will be flattened out. This is implementation detail of the React.createElement method. JSX only gives you the syntactic sugar to work with it.

More implementation details can be found here: https://reactjs.org/docs/react-api.html#createelement

By the way this comes very handy when programming in React. Even when there are no elements in your array at all it won't render anything and your app won't crash because map returns an empty array which resolves to no children at this point.

Happy coding!

Thank you Ryan & Torben for your excellent replies!

Great question and great answer! Dude deserves a "Best answer"