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()

Using Explicit Return for this lesson

My code works fine when using an explicit return like the instructor, but for my personal preferences I like seeing the () parenthesis and {} curly braces. But for some reason my UI won't render with explicit return.

Can anyone tell me how to use explicit return for this? Thanks in Advance!

My Explicit Return (Doesn't Work)

{ props.initialPlayers.map( ( player ) => {
     <Player 
         name={ player.name }
         score={ player.score} 
     />
}) }

Instructors Implicit Return (Works)

{ props.initialPlayers.map( player =>
    <Player 
        name={ player.name }
        score={ player.score} 
    />
) }

1 Answer

Nevermind, I finally figured it out. I never realized you need to use return keyword and a second set of () parenthesis.

This works

{ props.initialPlayers.map( ( player ) => {
     return (
         <Player 
             name={ player.name }
             score={ player.score} 
         />
     );
}) }

Looking at this now it doesn't look as nice and I see why Guil used an implicit return. It looks much nicer.

Yes. You're right. Good find You can learn more about the brevity of this fat arrow syntax here: https://teamtreehouse.com/library/introducing-arrow-function-syntax