0

Found my mistake:

i did several asychronous requests in a for loop. and in each loop I had an async/await case. The problem: the first successful await triggered the rerender. Solution: I put the for loop in a separate function which returns the end result of the for loop. Then await this function result in the main function.

i have a problem in ReactJS. I have an Array, which is filled:

const pickedAddresses = useSelector(state => state.map.pickedAddresses); // Redux Stateslice    
console.log("AddressesArray:", pickedAddresses, "AddressesArray.length:", pickedAddresses.length);

returns:

AddressesArray: []                                            AddressesArray.length: 0
0: "Biberstraße 4, 50678 Köln, Deutschland"
1: "Alteburger Str. 40, 50678 Köln, Deutschland"
2: "Im Klapperhof 22-24, 50670 Köln, Deutschland"
3: "Kleiner Griechenmarkt 38, 50676 Köln, Deutschland"
4: "Sechtemer Str. 10, 50968 Köln, Deutschland"
length: 5
__proto__: Array(0) 

I was trying to map over this Array, but nothing happens.

Anyone an idea?

Thank you.

In addition, the screenshot of my Redux State:

Redux_State

  • This is basic async/await. You don't have data until your api call completes and the data is returned. You need to handle when you do and do not have data in your state. – crashmstr Feb 22 '21 at 19:56
  • Does this answer your question? [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) – crashmstr Feb 22 '21 at 20:40
  • But the data is there. The Array is logged with full content. The data is written to state in “then” block from asynchronous call. Only the .length is returning 0 – Sven Hager Feb 23 '21 at 06:41
  • You do not show any code with a `.then`, so perhaps you need to show what code you have and where the problem with the data is within that code. (your block of "data" shows a `[]` and a `.length: 0` but also shows an array with values and `length`, so which is it?) – crashmstr Feb 23 '21 at 12:22

0 Answers0