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 Provide and Consume Actions

Tyler McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tyler McDonald
Full Stack JavaScript Techdegree Graduate 16,700 Points

What is the point of "actions" in the context object?

Why do we need that additional layer within the context object? Is that a convention/best practice when using context?

For example instead of this:

        value={{
          players: this.state.players,
          actions: {
            changeScore: this.handleScoreChange,
            removePlayer: this.handleRemovePlayer,
            addPlayer: this.handleAddPlayer,
          },
        }}

Why don't we do this, to cut down a layer of the object?

        value={{
          players: this.state.players,
          changeScore: this.handleScoreChange,
          removePlayer: this.handleRemovePlayer,
          addPlayer: this.handleAddPlayer,
        }}

1 Answer

Kevin D
Kevin D
8,646 Points

I guess it provides more context to those properties. Changing score, removing a player, and adding a player can all be considered an "action". So when you end up calling that function, it provides a little more meaning to write: "actions.changeScore" over just calling "changeScore"