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 (retired) Designing Data Flow Restructuring State

Francis N
Francis N
10,376 Points

this.state.players.map(function(player){...}? Where does players come from if we changed players to initialPlayers?

Where does players come from if we changed players to initialPlayers?

3 Answers

Juan Pablo Pinto Santana
Juan Pablo Pinto Santana
15,719 Points

Hello!

As you see in the video, the Application component receives the initialPlayers prop. Inside the component you can see that the getInitialState method enables to set the initial state value of players (players is a property of the state object) to initialPlayers like this:

getInitialState: function() {
    return {
        players: this.props.initialPlayers
    };
}

Happy coding!

Arturo Goicochea
Arturo Goicochea
8,077 Points

That makes sense, but why wasn't players defined inside of the propTypes of the application component?

It is also 2 times in 2 components (header and stats).

players doesn't need to be defined as a propType because it's not an actual property param of the component.

examples: <Player name={} score={} key={} />, <Counter score={} />

For Application we just want to give it an initial list of players....

So, defined propTypes are things that would be passed in as arguments for the component i.e initalPlayers here and accessible by the PROPS object. this.props.initialPlayers;

<Application initialPlayers={} />

In order to properly change the state of an object or variable we need to access the STATE object for the component.

He is using an object named players as a secondary array to change state with. this.STATE.players. This makes the code a little more readable and understandable.

"Give the application an initial list of players called initialPlayers. The application should be able to make changes to the state of players."

We are just assigning an object named players to state, that name could be whatever we want and we are able to access it from the state object this.state.variableName.

We can then reset the players object with this.setState({ players: this.props.initialPlayers });

Keith Carrillo
Keith Carrillo
6,909 Points

Hey Arturo Goicochea you ever find out why it wasn't defined in the proptypes?