简体   繁体   English

如何创建一个实时的 React 应用程序?

[英]How to create a real-time React application?

I want to create a real-time news application.我想创建一个实时新闻应用程序。 Just think of a list of countries and the news about that country next to the country name.想想国家列表和国家名称旁边有关该国家的新闻。 But the news column should be automatically updated when new news is added to the API.但是当新消息添加到 API 时,新闻栏应该会自动更新。 For Example, I have this endpoint: https://newsdata.io/api/1/news?apikey=YOUR_API_KEY&country=au In this case, the country is Austria.例如,我有这个端点: https://newsdata.io/api/1/news?apikey=YOUR_API_KEY&country=au在这种情况下,国家是奥地利。 How can my react application automatically checks the server and fetch the data if there is an update?如果有更新,我的反应应用程序如何自动检查服务器并获取数据? I don't want to use setInterval to check it in specific time ranges, can socket io be used in this case?我不想使用 setInterval 在特定时间范围内检查它,这种情况下可以使用套接字 io 吗?

You can use "socket.io-client" for your react app and "socket.io" for your node server.您可以为您的反应应用程序使用“socket.io-client”,为您的节点服务器使用“socket.io”。 Your server can raise an event that the react app can handle with the help of "socket.io-client" after each update of the API在每次更新 API 后,您的服务器可以在“socket.io-client”的帮助下引发反应应用程序可以处理的事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM