简体   繁体   English

如何保持 React 组件与后端同步?

[英]How to keep a React component in sync with backend?

I'm working on a small React application that uses Redux for state management.我正在开发一个使用 Redux 进行状态管理的小型 React 应用程序。

The table below displays a dynamic list of objects, which are retrieved from the REST backend, which is implement in Java with Spring.下表显示了一个动态的对象列表,这些对象是从 REST 后端检索的,它是用 Java 和 Spring 实现的。 Currently, I have to click a button in order to add the latest data delta (to the Redux store).目前,我必须单击一个按钮才能添加最新的数据增量(到 Redux 存储)。

带加载按钮的动态表

The table should update automatically in a performant way.该表应该以一种高效的方式自动更新。 I found a hacky workaround that recursively used Window's setTimeout method to periodically fetch data from the backend, but I did not like the solution.我发现了一个 hacky 解决方法,它递归地使用Window 的 setTimeout方法定期从后端获取数据,但我不喜欢这个解决方案。


What frameworks, tools, or approaches can I use for auto-updating that integrate well with React, Redux, React Redux, and Redux Thunk?我可以使用哪些框架、工具或方法与 React、Redux、React Redux 和 Redux Thunk 很好地集成来进行自动更新?

Since you're already using redux and react-redux, if an action is dispatched and the store's state is updated, the component should be rerendered with the new data.由于您已经在使用 redux 和 react-redux,如果一个 action 被调度并且 store 的 state 被更新,组件应该用新数据重新渲染。
When you call setTimeout to periodically fetch data, you're using a technique called polling.当您调用setTimeout来定期获取数据时,您正在使用一种称为轮询的技术。
To avoid the need to do polling, it's also up to the backend, whether you support WebSocket or GraphQL's subscription or using some kind of real-time datasource (eg Firebase)为了避免轮询的需要,这也取决于后端,是否支持 WebSocket 或 GraphQL 的订阅或使用某种实时数据源(例如 Firebase)

Using window.setInterval is better than window.setTimeout for this purpose.为此,使用window.setIntervalwindow.setTimeout更好。 Other than fetching periodically data from your client, you could look into a websockets library such as socket.io although this will need configuration server-side.除了定期从客户端获取数据之外,您还可以查看websockets库,例如socket.io,尽管这需要配置服务器端。

If you are talking about auto-updating data reactively - when something in your database is updated - you need some kind of socket server for that.如果您正在谈论被动地自动更新数据 - 当您的数据库中的某些内容更新时 - 您需要某种套接字服务器。 You can fire an event from your backend, and subscribe to it in your frontend, and then perform a query to fetch the data.您可以从后端触发事件,并在前端订阅它,然后执行查询以获取数据。 I don't think using setInterval is a good idea for this type of stuff (in most cases).我不认为使用setInterval是这类东西的好主意(在大多数情况下)。

Check out Pusher .看看Pusher

May be CouchDB (or Couchbase (it's not the same) with PouchDB could help? I what try it in couple of days.可能是 CouchDB(或 Couchbase(它不一样)与 PouchDB 有帮助吗?我在几天内尝试一下。

Seems they have Spring Data libraries似乎他们有 Spring Data 库

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

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