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 React Basics (2018) 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:


(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
.a{fill-rule:evenodd;}techdegree seal-36
Torben Korb
Front End Web Development Techdegree Graduate 91,237 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"