Last active
December 23, 2018 13:15
-
-
Save PhilipFlyvholm/d4171a16900cef6146b097a7ed432515 to your computer and use it in GitHub Desktop.
Example of RSS feed as JSON in React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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