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 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 Staff

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.