1

I am trying to create a currency converter and I want to show a list with the currency and the value. I am getting some data using axios from an API and my data looks like this:

Object {type: "li", key: "0", ref: null, props: Object, _owner: null…}
type: "li"
key: "0"
ref: null
props: Object
 children: Array[33]
 0: Object
  name: "AUD"
  value: 1.5167896679
 1: Object
 2: Object

...

When I try to map the data it is not showing anything:

import React from "react";
import axios from "axios";

class Table extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      result: null,
      fromCurrency: "USD",
      currencyList: []
    };
  }

  componentDidMount() {
    this.getCoinTypes();
  }

  getCoinTypes() {
    let currency = [];

    axios
      .get(
        `https://alt-exchange-rate.herokuapp.com/latest?base=${
          this.state.fromCurrency
        }`
      )
      .then(response => {
        currency.push(
          Object.keys(response.data.rates).map(k => ({
            name: k,
            value: response.data.rates[k]
          }))
        );

        this.setState({
          currencyList: currency
        });

        this.state.currencyList.map((val, index) => {
          return console.log(<li key={index}>{val}</li>);
        });
      })
      .catch(function(error) {
        console.log(error);
      });
  }

  render() {
    return (
      <table className="table table-sm bg-light mt-4">
        <thead>
          <tr>
            <th scope="col" />
            <th scope="col" className="text-right pr-4 py-2">
              5.00
            </th>
          </tr>
        </thead>
        <tbody>
          {this.state.currencyList.map((val, index) => {
            <tr key={index}>
              <th>{index}</th>
              <th>{val}</th>
            </tr>;
          })}
        </tbody>
      </table>
    );
  }
}

export default Table;

What I am want to achieve is e.g:

  • USD 1.00
  • AUD 1.95

and so on...

The link to sandbox

7
  • You need a render function to get your data into the browser. Is React not throwing errors about your class? Commented Feb 25, 2020 at 20:41
  • I am working in codesanbox, and using console.log to see how it looks Commented Feb 25, 2020 at 20:43
  • Can you attach a link to your answer so we have all the code to work with? Commented Feb 25, 2020 at 20:43
  • Sure! this is my link: codesandbox.io/s/react-props-practice-t8dtu Commented Feb 25, 2020 at 20:44
  • 1
    Can I recommend adding that link to your original post? It helps us dense stack exchange users. Commented Feb 25, 2020 at 20:48

2 Answers 2

1

Is this what you needed? https://codesandbox.io/s/react-props-practice-tsvbu

If yes, what I did was to save the currency "label" (USD, AUD, etc) and it's "value" (1.00, 1.69, etc)

Sign up to request clarification or add additional context in comments.

2 Comments

Throwing an error for non-unique keys. I suggest concatenating the array index with the currency label for a key.
Updated sandbox. Fixed error for non-unique keys. Cleaned up Table.jsx
0

Here is a working code sandbox

https://codesandbox.io/embed/react-props-practice-94t49?fontsize=14&hidenavigation=1&theme=dark

Changed this on line 60 of Table.jsx

          {this.state.currencyList.map((val, index) => {
            return (
              <tr key={index}>
                <th>{index}</th>
                <th>{val.value}</th>
              </tr>
            );
          })}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.