Skip to content

Instantly share code, notes, and snippets.

@mittalyashu
Created April 8, 2018 06:54
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mittalyashu/4182b0cdaa54654e6b9f3e9441831edc to your computer and use it in GitHub Desktop.
Save mittalyashu/4182b0cdaa54654e6b9f3e9441831edc to your computer and use it in GitHub Desktop.
Fetch Data From RSS Feed In React
class FetchDataFromRSSFeed extends Component {
constructor() {
super();
this.state = {
recentBlogPost: {
name: '',
url: ''
}
}
}
FetchDataFromRssFeed() {
var request = new XMLHttpRequest();
request.onreadystatechange = () => {
if (request.readyState == 4 && request.status == 200) {
var myObj = JSON.parse(request.responseText);
for (var i = 0; i < 1; i ++) {
this.setState({
recentBlogPost: {
name: myObj.items[i].title,
url: myObj.items[i].link
}
});
}
}
}
request.open("GET", "https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Fblog.codecarrot.net%2Ffeed.xml&order_dir=desc&count=30", true);
request.send();
}
componentDidMount() {
{this.FetchDataFromRssFeed()}
}
render() {
return (
<div>
Check out our blog: <a target="_blank" href={this.state.recentBlogPost.url}>{this.state.recentBlogPost.name}</a>
</div>
);
}
}
@courage173
Copy link

please if the rss feed update will the converter update it too when i use it?

@jrodg
Copy link

jrodg commented Aug 31, 2020

Hi mittalyashu, great work this is very helpful. I ran your script but I'm getting cannot read property 'name of undefined. Can you help

@jrodg
Copy link

jrodg commented Sep 1, 2020

Hi how do you display more than one news

@nhanth19
Copy link

Hello Any one.. Please help to get the more then one website news on my react js.

hello, sorou7
Do you have a solution yet..?

@mittalyashu
Copy link
Author

@maggymoller Found an old video I created long ago https://www.youtube.com/watch?v=VlgyGFakEO8.

Hope that helps

@mtanner161
Copy link

super helpful - thanks for showing me this got an RSS working with this code help. Question is now how do I get more than 1 to show up? Just loop through items?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment