0

I am working on a react app using Node.js.

I need to simplify my component so I took out the fetch calls and put them into their own file (dataDown.js) and then imported them into the file.

When I try to update the state the data has downloaded yet. So how should I down get the data from external file making the fetch call. downdata is an object with two arrays in it

`fetch(urlRecent).then((response) => {

response
  .json()
  .then((data) => {
    dataDown.recent = data
  })
})
fetch(urlAllTime).then((response) => {

  response
    .json()
    .then((data) => {
      dataDown.alltime = data
    })
}).catch((err) => {
  console.log('Fetch Error :-S', err)
})

export default dataDown`

then i import it

import dataDown from '../../data/downData'

Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Mayank Shukla May 29 '17 at 05:52

1 Answers1

1

Make a function out of the hole fetch-Call, make it return the dataDown Object and import the function.

That way you can rely on the data you are getting.

Björn Böing
  • 1,662
  • 15
  • 23