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>
);
}
}
@souru7
Copy link

souru7 commented Jul 30, 2019

that's not working for my react js project
I didn't see any news ??

@mittalyashu
Copy link
Author

@souru7, you need to convert your RSS feed into JSON format and then fetch the data.

@souru7
Copy link

souru7 commented Jul 30, 2019

Thanks... Now Working

@souru7
Copy link

souru7 commented Jul 31, 2019

Hello Mittalyashu,

How to display more than one news and can i get multiple website news at a time.

Thanks in Advance.

@souru7
Copy link

souru7 commented Aug 8, 2019

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

@mittalyashu
Copy link
Author

Hey @souru7,

To get the news (data) from multiple website will be same, as you do with single website, fetch the data in RSS format, convert it into JSON and use the above code snippet 🤟🏻.

@maggymoller
Copy link

How do you convert data from RSS format to JSON? Can you do a video on that?

@mittalyashu
Copy link
Author

@maggymoller You can use this handy tool www.rss2json.com

@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