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

Norman Lew
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Norman Lew
Full Stack JavaScript Techdegree Graduate 14,784 Points

Why is return required in this block of code?

handleAddPlayer = (name) => { this.setState( prevState => { return { players: [ ...prevState.players, { name, score: 0, id: this.prevPlayerId += 1 } ] } }); }

1 Answer

Tim Oltman
Tim Oltman
7,730 Points

Hi Norman,

If you want to implicitly return an object literal you need to wrap it in parentheses. For example,

var func = (name) => ({ myName: name })

func("Tim") // { myName: "Tim" }

In your code above, the outer braces are the start of the function's code block. Without a return statement it doesn't know what you want to return.