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?