Skip to content

Instantly share code, notes, and snippets.

@mjunaidi
Forked from PhilipFlyvholm/rssFeed.js
Created December 23, 2018 13:15
Show Gist options
  • Save mjunaidi/6fb929a9039b64ef2ba2efbcdeb04317 to your computer and use it in GitHub Desktop.
Save mjunaidi/6fb929a9039b64ef2ba2efbcdeb04317 to your computer and use it in GitHub Desktop.
Example of RSS feed as JSON in React Native
import React from 'react';
import {StyleSheet, View, Text} from 'react-native';
export class RSSFeed extends React.Component{
constructor(props) {
super(props);
this.state = {
rss: []
};
this.setRss = this.setRss.bind(this);
}
setRss(json) {
this.setState({
rss: json.items
});
}
render () {
return (
<View style={styles.container}>
{this.state.rss.map(item =>
<View key={item.guid}>
<Text style={styles.textBold}>{item.title}</Text>
<Text>{item.content}</Text>
</View>
)}
</View>
)
}
componentDidMount() {
const parseUrl = 'https://api.rss2json.com/v1/api.json?rss_url=';
const rssUrl = 'https://www.dr.dk/nyheder/service/feeds/allenyheder/';
fetch(parseUrl + rssUrl)
.then(response => response.json())
.then((json) => {
if (json.status === 'ok') {
this.setRss(json);
}else {
console.log("failed");
}
});
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: 10
},
textBold:{
fontWeight: 'bold'
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment