-1

so i am using an exchange rate api to display the different currency rates. I initially set the state of my exchange rates to null. I then use useEffect to get the data and then add the (key,value) of (currency, currency_value) to an array called fx_rates. I then use setExchangeRates to change it to fx_rates but it does not update.

This is my App.js


import './App.css';
import ExchangeRates from './components/exchangeRates.jsx';


function App() {

  return (
    <div className="App">
    <h1>World of Exchange</h1>
    <ExchangeRates/>
    </div>
  );
}

export default App;

and this is my exchangeRates.jsx

import React, {useEffect, useState} from 'react'
import axios from "axios"

function ExchangeRates() {

  const [exchangeRates, setExchangeRates] = useState([]);
  
  useEffect(() => {
    getExchangeRates()
  }, [])

  const getExchangeRates = async() => {
    const res = await axios.get(` https://v6.exchangerate-api.com/v6/${process.env.REACT_APP_API_KEY}/latest/USD`)
    const data = res.data;
    const converted_rates = data.conversion_rates;
    // envrionmental variable as API Key
    // when the site is first loaded the base currency will be USD
    const fx_rates = []
    for(const [currency, currency_value] of Object.entries(converted_rates)){
      
    fx_rates.push({currency, currency_value})
    }
     // the line above splits the splits the hash into an array each consisting of the key value pairs
     setExchangeRates(fx_rates)
     console.log(exchangeRates)
  }



  return (
    <div>
     

      
     
  
    </div>
 
   
   
    
  );
}

export default ExchangeRates

the console log shows that the state is still null. Also for some reason axios sends an api request two or three times. if someone can help me that would be amazing

  • Don't worry about the console log, just map over `exchangeRates` in your JSX and it'll work fine. – ChrisG May 14 '22 at 20:23
  • Regarding the fact that you have multiple api calls and if you are using React 18 [this](https://stackoverflow.com/questions/72238175/react-18-useeffect-is-getting-called-two-times) would help. – yousoumar May 14 '22 at 20:32
  • This is the hook dupe btw: https://stackoverflow.com/questions/54069253/the-usestate-set-method-is-not-reflecting-a-change-immediately – ChrisG May 14 '22 at 20:33
  • @ChrisG i did map over it and nothing came up – Monkey D Luffy May 14 '22 at 20:40
  • Start by replacing `console.log(exchangeRates)` with `console.log(fx_rates)` – ChrisG May 14 '22 at 20:43
  • @yousoumar The strict mode thing is [not specific to React 18](https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects) – ChrisG May 14 '22 at 20:44
  • I know that strict mode is not realeateed to React 18 @ChrisG. I'm talking about this : *With Strict Mode starting in React 18, whenever a component mounts in development, React will simulate immediately unmounting and remounting the component*. Witch might cause in his case multiples api calls. – yousoumar May 14 '22 at 20:51

0 Answers0