I'm trying to use react-native-rss-parser
(https://www.npmjs.com/package/react-native-rss-parser) to parse my RSS feed and display it on a card but I don't know how to access the rss
variable.
fetchRSSFeed() {
return fetch('http://www.nasa.gov/rss/dyn/breaking_news.rss')
.then((response) => response.text())
.then((responseData) => rssParser.parse(responseData))
.then((rss) => { console.log(rss.title) })
}
render() {
{this.fetchRSSFeed()}
return (
<SafeAreaView style={styles.container}>
<Swiper
cards={HomeScreenPics}
renderCard={Card}
infinite
backgroundColor="white"
cardHorizontalMargin={0}
stackSize={2}
/>
</SafeAreaView>
)
}
}
HomeScreenPics = [{
pic: require('../assets/images/news.jpg'),
title: rss.title
}]
console.log(rss.title)
works fine. It logs out the RSS feed title but I'm trying to access the rss
varible outside the fetch method, I want to parse it into my HomeScreenPics
array, but it keeps showing the error: Can't find variable rss
If you want to access the rss variable and pass it to the render function, all you have to do is to use the state that react provides you.
Example:
state = {
feed: {}
}
fetchRSSFeed() {
return fetch('http://www.nasa.gov/rss/dyn/breaking_news.rss')
.then((response) => response.text())
.then((responseData) => rssParser.parse(responseData))
.then((rss) => {
this.setState(prevState => ({
...prevState,
feed: rss
}));
})
}
render() {
{this.fetchRSSFeed()}
return (
<SafeAreaView style={styles.container}>
<Swiper
cards={HomeScreenPics}
renderCard={Card}
infinite
backgroundColor="white"
cardHorizontalMargin={0}
stackSize={2}
/>
<Text>{this.state.feed.title}</Text>
</SafeAreaView>
)
}
}
You can access the response object with the variable this.state.feed
in any function.
I recommend you to start from the official documentation: [https://facebook.github.io/react-native/docs/state]
Friendly.