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 (2018) Introducing Props Use Props to Create Reusable Components

Otto linden
Otto linden
5,857 Points

Can't you just do score={50} once?

Hello! Why do you need to do the score={}twice? once in the app, player and once in the player? cant you just do it in player? :) My code:

const Header = (props) => {
  return (
    <header>
      <h1>{props.title}</h1>
      <span className="stats">Players: { props.totalPlayers }</span>
    </header>
  );
}

const Player = (props) => {
  return (
    <div className="player">
      <span className="player-name">
        { props.name }
      </span>

      <Counter score={35}/>
    </div>
  );
}

const Counter = (props) => {
  return (
    <div className="counter">
      <button className="counter-action decrement"> - </button>
      <span className="counter-score">{ props.score }</span>
      <button className="counter-action increment"> + </button>
    </div>
  );
}

const App = () => {
  return (
    <div className="scoreboard">
      <Header title="scoreboard" totalPlayers={1} />

      {/* Players list */}
      <Player name="Guil" />
    </div>
  );
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

3 Answers

JS Goupil
JS Goupil
11,309 Points

I know this question is kinda old, but just for the sake of others that might be reading this, you are correct. If we add more players to the app, and the score is hard coded in the Player component, all players added will have the same score. So now instead of hardcoding it in the player, we can set it individually for each player component that is added. Hope that helps :)

Otto linden
Otto linden
5,857 Points

is ti because we add more people to the app?

Hi Otto,

I suggest the Player component used in the App component is responsible for setting the initial counter value via attributes. The Counter component within the Player component just receives the counter value through props.