0

I am fairly new to React Native, so bear with me.

I use the following code to get RSS items form a feed.

fetch("https://feed.podbean.com/omroepvoeren/feed.xml")
  .then((response) => response.text())
  .then((responseData) => rssParser.parse(responseData))
  .then((rss) => {
    for (var item of rss.items) {
      console.log(item.title);
    }
  });

This seems to work. All titles are shown in the console. But when I change the code to this:

var listOfTitles = [];
fetch("https://feed.podbean.com/omroepvoeren/feed.xml")
  .then((response) => response.text())
  .then((responseData) => rssParser.parse(responseData))
  .then((rss) => {
    for (var item of rss.items) {
      listOfTitles.push(
        <View>
          <Text>{item.title}</Text>
        </View>
      );
    }
  });

console.log(listOfTitles); 

The array remains empty. Array [] What is going wrong here?

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
AgeDeO
  • 3,137
  • 2
  • 25
  • 57

1 Answers1

0
var listOfTitles = [];
    fetch("https://feed.podbean.com/omroepvoeren/feed.xml")
      .then((response) => response.text())
      .then((responseData) => rssParser.parse(responseData))
      .then((rss) => {
        for (var item of rss.items) {
          listOfTitles.push(
            <View>
              <Text>{item.title}</Text>
            </View>
          );
        }

        console.log(listOfTitles); 
      });
const response = await fetch("https://feed.podbean.com/omroepvoeren/feed.xml");         
const responseText = await response.text();                                                                    const rss = rssParser.parse(responseText);                                                                      
for (var item of rss.items) {
    listOfTitles.push(
        <View>
             <Text>{item.title}</Text>
        </View>
    );
}                                                                                                                                          console.log(listOfTitles);

It would pass fetch function, but you are adding items after calling api done. So it would be empty array, if you want, you can use "await" too.

Wang YinXing
  • 278
  • 1
  • 6