Search code examples
reactjsconstantsexportfetch-api

How to export a variable after fetching data from API in reactjs?


I need to export a const from a jsx file. Currently it is set to static values and is working fine.

Now the problem is I need to pass data, for this const, from the API.

So, how can I export this const after getting data from the API call?

I've tried below code but it is not working!

let response = async()=>{ return fetch("http://localhost:54982/api/Admin/GetRouteLinks")  }
let resData = async()=>{ return response.json() }

let dashRoutes = resData;
export default dashRoutes;

Solution

  • you can export a function (getData) is ur Ajax data request

    import {getData} from '';
    
    export const nameFunc = () => {
       getData().then(data => {
           /* do what you want to do in promise resolve callback function */
       })
    }