Michael Roberts Michael Roberts - 15 days ago 6
React JSX Question

How to parse some data in React.js

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.

Hopefully someone has wanted to manipulate data in this way before...

GG. GG.
Answer

The parseFloat function accepts only a string as argument. In your example, you are passing an object ({} syntax).

I don't know exactly what you are trying to achieve, but you can parse your data in the React render function:

class Module extends React.Component {     

    render() {

        // The parsing
        var usdgbp = this.state.quotes ? 
            parseFloat(this.state.quotes.USDGBP) :
                null; // or 0, or '', or something else :)

        // Some extra tricky stuff
        usdgbp = usdgbp + 2;

        // Then you display
        return (
            <div>
                {usdgbp}
            </div>
        );
    }

}
Comments