0

We tried to obtain weather data, but tried with the react-async library to determine the return value of this asynchronous function. However, the status is always displayed as pending and no data is defined. Can you tell me why?


// The parameter was omitted because the required values were obtained through other functions.
const weatherData = async () => {
  let req = await axios.get(getURLs(...getTime()).curr);
  let reqData = current(req);
  // console.log(req); 
  // console.log(reqData);
  return reqData;
};


// Component function
const WeatherList = () => {
  const { data, error, isLoading, promise } = useAsync({
    promiseFn: weatherData,
  });
  console.log(data); // undefined << Why are you doing this to me?
// ... more code
}

react-async docs : https://docs.react-async.com/guide/async-components

최석규
  • 46
  • 5
  • 1
    Because the request hasn't been completed yet :) just ensure when you don't have data, return null, and once it's true, it will re-render and you'll have data – Icepickle Dec 07 '22 at 15:26
  • 1
    What your "current" function is doing to the request parameter ? What happen when you uncomment the console.log(req)? – Nicolas Menettrier Dec 07 '22 at 15:33
  • 1
    Double-check the docs https://usehooks.com/useAsync/, `data` is undefined because you do not have an initial value set. – evolutionxbox Dec 07 '22 at 15:48

0 Answers0