I am printing the data fetched from an API into a table, and I am facing some difficulties to fix the rows numerical values to decimals. If the rows data within the API consists of numerical values, i.e. 10.0, 333, 8 or 100
, etc. to render it in the end with decimal values -> 10.00, 333.00, 100.00
.
The function that I am familiar with .toFixed(2)
, doesn't function in the same way in React, as I used to code it in javaScript. I think I am misleading the ES6 standard, but I am not sure.
Here is how it's look like, if I avoid using .toFixed(2)
.
Here is my code sample with rows.toFixed(2)
, but it's doesn't function well:
class App extends React.Component
{
constructor()
{
super();
this.state = {
rows: [],
columns: []
}
}
componentDidMount()
{
fetch( "http://ickata.net/sag/api/staff/bonuses/" )
.then( function ( response )
{
return response.json();
} )
.then( data =>
{
this.setState( { rows: data.rows, columns: data.columns } );
} );
}
render()
{
return (
<div id="container" className="container">
<h1>Final Table with React JS</h1>
<table className="datagrid">
<thead>
<tr> {
this.state.columns.map(( column, index ) =>
{
return ( <th>{column}</th> )
}
)
}
</tr>
</thead>
<tbody> {
this.state.rows.toFixed(2).map( row => (
<tr>{row.toFixed(2).map( cell => (
<td>{cell}</td>
) )}
</tr>
) )
}
</tbody>
</table>
</div>
)
}
}
ReactDOM.render( <div id="container"><App /></div>, document.querySelector( 'body' ) );
You are welcome to contribute directly to my Repo: Fetching API data into a table
Here is how looks like my example, when the .toFixed
has been used:
Unfortunately I was not able to find related documentation at ReactJs.org
It's doesn't function with rows[].length.toFixed(2)
either.
Any suggestions will be appreciated!