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 Components Managing State Building the Statistics Component

Seth wright
Seth wright
952 Points

Why declare a variable for the total number of players instead of putting the data inline?

Why this:

const Stats = (props) => {
const totalPlayers = props.players.length;
  return (
    <table className="stats">
      <tbody>
        <tr>
          <td>Players:</td>
          <td>{totalPlayers}</td>
        </tr>
        <tr>
          <td>Total Points:</td>
          <td>{props.score}</td>
        </tr>
      </tbody>
    </table>
  );
};

Instead of this:

const Stats = (props) => {
  return (
    <table className="stats">
      <tbody>
        <tr>
          <td>Players:</td>
          <td>{props.players.length}</td>
        </tr>
        <tr>
          <td>Total Points:</td>
          <td>{props.score}</td>
        </tr>
      </tbody>
    </table>
  );
};

Is this just a stylistic choice or is there a practical reason? They seem to work the same.

1 Answer

Travis Alstrand
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Travis Alstrand
Treehouse Teacher

Hey Seth wright ! You're correct, both will work just fine. I think that the top option makes the JSX more readable and if you were to need to access props.players.length again, it would save you from needing to type all of that out multiple times.