Home > Software design >  axios and react-native api data export problem
axios and react-native api data export problem

Time:07-16

I'm trying to export the data that I pulled from axios in my stock market tracking software from a js file and import it to the main screen of my project, but the output is not what I want. I want to print the data on my main screen, but I get a promise output.

api.js

import axios from "axios";


 async function fetchApi(){
    const response = await axios.get("http://hasanadiguzel.com.tr/api/kurgetir");
    return  response.data
} 
export default fetchApi;

main.js

import fetchApi from "../api/fethApi";
    const [dovizData, setDovizData] = useState();

    const getJSON = () =>{
        setDovizData(fetchApi());
        console.log(dovizData);
    }
    setTimeout(getJSON,3000)

Output

Promise {<fulfilled>: Array(23)}

CodePudding user response:

use Async/await to get promise data

import fetchApi from "../api/fethApi";

const [dovizData, setDovizData] = useState();

const getJSON = async () => {
  const res = await fetchApi();
  setDovizData(res);
  console.log(dovizData);
}
setTimeout(getJSON, 3000)

  • Related