Search code examples
javascriptreactjsreact-props

How to sum two React props?


What I wanna do is sum two React props that contain values that the App is fetching from a database

Here's the props snippet

const GPBBatting = (props) => (
  <tr>
    <td>{props.player.Player}</td>
    <td>{props.player.G_GS.substring(0, 1)}</td>
    <td>{props.player.AB}</td>
    <td>{props.player.R}</td>
    <td>{props.player.H}</td>
    <td>{props.player.H2}</td>
    <td>{props.player.H3}</td>
    <td>{props.player.HR}</td>
    <td>{props.player.RBI}</td>
    <td>{props.player.SB_ATT}</td>
    <td>{props.player.BB}</td>
    <td>{props.player.SO}</td>
    <td>{props.player.BA}</td>
    <td>{props.player.OBP}</td>
    <td>{props.player.SLG}</td>

    <<<<<<I want the sum of OBP + SLG values here>>>>>

    <td>{props.player.TB}</td>
    <td>{props.player.SH}</td>
    <td>{props.player.SF}</td>
  </tr>
);

And here in this link is the complete code: https://www.paste.org/111710


Solution

  • Isn't this just <td>{props.player.OBP + props.player.SLG}</td>? Or if they're not numbers, <td>{parseInt(props.player.OBP) + parseInt(props.player.SLG)}</td>.

    I'd recommend brushing up on your JS with something like learnxinyminutes or Mozilla's excellent JS primers.