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

Tyler McDonald
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tyler McDonald
Full Stack JavaScript Techdegree Graduate 16,676 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,634 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"