1

I'm attempting to do some manipulations on array values that I have obtained from an API query within a React.js environment. However, these array values are numbers that are stored as "strings", e.g., "1.02011". I'm now attempting to take the inverse of this value as I wish to do some extra tricky stuff with it.

So, I have: var usdgbp = parseFloat({this.state.quotes ? this.state.quotes.USDGBP : null});

{this.state.quotes ? this.state.quotes.USDGBP : null} is something which is working fine elsewhere in the code...so, I'm thinking - what is the correct way of parsing the number out from a string of this nature/type, and within the React.js class Module extends React.Component {} setup.

GG.
  • 21,083
  • 14
  • 84
  • 130
  • Not sure about your question but `parseFloat(null)` == NaN so returning null in your else seems odd. And your putting {} around the value? why? – Taplar Nov 19 '16 at 16:31
  • @Taplar It's a react.js prop when it is encolsed in {}. –  Nov 19 '16 at 16:37
  • Ok, well still parsing null seems odd. You should probably return '0' maybe instead of null. – Taplar Nov 19 '16 at 16:38
  • @Taplar so this is something I was just told off the bat. What exactly does `: null` mean in React.js? I was under the impression that it checks if it is *NOT* a null value. I'm using it to access the string value from the JSON data, which works fine. –  Nov 19 '16 at 16:44
  • 1
    This is not really a special "React.js" syntax - what you have is a [ternary operator](http://stackoverflow.com/questions/6259982/how-do-you-use-the-conditional-operator-in-javascript) inside `{}` which is not really valid JS. – VLAZ Nov 19 '16 at 16:48
  • `trueFalseCondition ? truthyResult : falseyResult` – Taplar Nov 19 '16 at 16:50

1 Answers1

0

The parseFloat function takes a string as argument, not an object (the {} syntax).

And you can parse your data in the component's render function:

class ExchangeRate extends Component {
  ...

  render() {
    const { quotes } = this.state        

    if (!quotes) return null

    return (<span>Exchange rate: {parseFloat(quotes.USDGBP)}</span>)
  }
}
GG.
  • 21,083
  • 14
  • 84
  • 130